Wednesday, March 20, 2019

Find files, list contents with line length maxium

I needed to parse a directory tree and list all the files of a particular type, list the directory name, print the first few lines of each file, with long line lengths trimmed. I also wanted to only get the particular types of lines out of that initial listing, which are ones with the date in the first few characters.

Here is my massive final command:
find . -name "*.csv" -exec echo {} \; -exec head -n 21 {} \; | cut -c -70 | grep 2018 | grep -v DATE | grep -v UTEND

The beautiful parts of this were: 1) two -exec functions for my find command. Yep you can do that. 2) The cut command 3) a chain of greps to get the date and cut out other lines that had the date that I didn't want.