# 问题内容:

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? If you replace the dollar with a space, it will work. @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


## 答案评论:

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.

## 答案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