Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: grep only lines with certain criteria

Tags:

grep

bash

awk

I am trying to grep out the lines in a file where the third field matches certain criteria. I tried using grep but had no luck in filtering out by a field in the file. I have a file full of records like this:

12794357382;0;219;215
12795287063;0;220;215
12795432063;0;215;220

I need to grep only the lines where the third field is equal to 215 (in this case, only the third line)

Thanks a lot in advance for your help!

like image 338
user1155413 Avatar asked Dec 13 '22 05:12

user1155413


2 Answers

Put down the hammer.

$ awk -F ";" '$3 == 215 { print $0 }' <<< $'12794357382;0;219;215\n12795287063;0;220;215\n12795432063;0;215;220'
12795432063;0;215;220
like image 103
Ignacio Vazquez-Abrams Avatar answered Dec 22 '22 17:12

Ignacio Vazquez-Abrams


grep:

grep -E "[^;]*;[^;]*;215;.*" yourFile

in this case, awk would be easier:

awk -F';' '$3==215' yourFile
like image 21
Kent Avatar answered Dec 22 '22 18:12

Kent