Home > Blockchain >  how to grep on a substring column
how to grep on a substring column

Time:10-17

I'd like to be able to grep/print the filename based on the 5th substring column. Only the .03. files in the below example.

dd.20211014.15000.123.03.11111.txt
dd.20211014.15203.143.03.11111.txt
dd.20211014.15404.123.01.11111.txt
dd.20211014.15033.126.06.11111.txt
dd.20211014.15000.123.03.11111.txt
dd.20211014.15011.323.04.11111.txt

CodePudding user response:

Assuming the layout of the line is consistent:

grep "\.03\."

CodePudding user response:

Try to list the files and grep:

ls | grep ".*03.*"

Or using find:

find . -name '*03*'

CodePudding user response:

As mentioned in the comments - awk should be the weapon of choice for this:

ls | awk -F. '$5=="03"'
dd.20211014.15000.123.03.11111.txt
dd.20211014.15203.143.03.11111.txt
dd.20211014.15000.123.03.11111.txt

If you prefer regex (in case you want to later on match 03 and 04 in one go, for instance:

ls | awk -F. '$5~/0[34]/'
dd.20211014.15000.123.03.11111.txt
dd.20211014.15203.143.03.11111.txt
dd.20211014.15000.123.03.11111.txt
dd.20211014.15011.323.04.11111.txt
  • Related