Reputation: 1141
I have a CSV file made up of a space delimiter and two columns.
I need to grep
any lines that end with either 06, 12, 18, or 00 in the first column.
file.txt
2017121106 22.9
2017121109 19.4
2017121112 17.2
2017121115 9999.0
2017121118 9999.0
2017121121 9999.0
2017121200 9999.0
2017121203 9999.0
2017121206 16.3
2017121209 13.1
2017121212 8.8
2017121215 8.1
2017121218 10.5
2017121221 8.6
Attempted Code:
egrep '(00|06|12|18)$' file.txt
Expected Output:
2017121106 22.9
2017121112 17.2
2017121118 9999.0
2017121200 9999.0
2017121206 16.3
2017121212 8.8
2017121218 10.5
I am getting an empty return when running this code in the terminal.
What am I doing wrong?
Upvotes: 3
Views: 75
Reputation: 67557
awk
to the rescue!
awk '!(substr($1,length($1)-1)%6)' file
will give
2017121106 22.9
2017121112 17.2
2017121118 9999.0
2017121200 9999.0
2017121206 16.3
2017121212 8.8
2017121218 10.5
you're looking the the multiples of 6 in the last two digits, translates into awk
as print line when remainder by dividing to 6 is zero
This solution works due to the domain of the data its 24h representation, so it won't have false positives due to other multiples of 6.
Upvotes: 3
Reputation: 11236
Using basic grep
grep '0[06] \|1[28] ' file
2017121106 22.9
2017121112 17.2
2017121118 9999.0
2017121200 9999.0
2017121206 16.3
2017121212 8.8
2017121218 10.5
Upvotes: 2
Reputation: 46886
So... The grep
command doesn't understand "fields". It only understands patterns. Since your "first column" is a pattern of numbers followed by a space, you'd match that instead of using $
:
$ egrep '^[0-9]+(00|06|12|18) ' file.txt
Note the space character at the end of the parenthesized expression. The initial [0-9]+
is in place so that we can anchor this regex to the beginning of the line, which makes sure we're matching the first "field".
A better solution might be to use awk
, which does understand fields:
$ awk '$1~/(00|06|12|18)$/' file.txt
Upvotes: 4
Reputation: 133750
Following awk
code may help you in same.
awk '(substr($1,length($1)-1)+0== 06 || substr($1,length($1)-1)+0 == 12 || substr($1,length($1)-1)+0 == 18 || substr($1,length($1)-1)+0 == 00 )' Input_file
Adding a non-one liner form of solution too now.
awk '
(substr($1,length($1)-1)+0== 06 ||\
substr($1,length($1)-1)+0 == 12 ||\
substr($1,length($1)-1)+0 == 18 ||\
substr($1,length($1)-1)+0 == 00 )
' Input_file
Upvotes: 1