Only Print Lines that End with Certain Numerical Values

问题内容:

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?

问题评论:

    
What is the logic regarding “06, 12, 18, or 00”? Should that list of values be generated programmatically?
    
it looks like every 6 hours, starting at midnight.
    
@karakfa – yes, just wanted confirmation. Perhaps timing should be different on weekends?
3  
If you replace the dollar with a space, it will work.
1  
@thanasisp, that would make “Dollar Store” an interesting place.

答案:

答案1:

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

答案评论:

1  
The anchor and [0-9]+ aren’t needed since it matches from the left anyway.
    
That would be the case if we were sure that there weren’t other data that the OP elected not to include in the sample and that all lines contain only a single space. I’m just being pedantic.
    
Surely if you were being pedantic it would be egrep '^[0-9]{8}(00|06|12|18) [0-9]+\.[0-9]$'
    
There are levels of pedantry. 🙂 To be truly pedantic, one would use something other than grep.

答案2:

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

答案评论:

答案3:

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.

答案评论:

    
Here, we’re splitting the field itself. If it had less digits !($1%6) would have worked as well.

答案4:

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

答案评论:

原文地址:

https://stackoverflow.com/questions/47755281/only-print-lines-that-end-with-certain-numerical-values

添加评论

友情链接:蝴蝶教程