简体   繁体   中英

grep: keep lines by number in specific column

I know how to do it with awk, for example, keep lines, which contains number 3 in second column: $ awk '"$2" == 3'

But how to do the same with only grep? What about for first column?

Grep is not great for this, awk is better. But assuming your columns are separated by spaces, then you want

grep -E '^[^ ]+ +3( |$)'

Explanation: find something that has a start of line, followed by one or more non-space characters (first column), then one or more space characters (column separator), then the number 3, then either a space (because there's another column) or end of line (if there's no other column).

(Updated to fix syntax after testing.)

Here is the longer explanation for my mysterious command grep -P '^[^\\t]*\\t3\\t' your_file from the comments:

I assumed that the column delimiter is a tab. grep without -P would require some strange things to use it directly (see eg see here ) . The -P makes it possible to just write \\t without any problems. If for example your delimiter is ; then you could replace the \\t with ; and you dont need the -P option.

Having said that, lets explain the idea behind the regular expression: You said, you want to match a 3 in the second column:

  • ^ means: at the beginning of the line
  • [^\\t]* means: zero or more ( * ) occurences of something not a tab ( [^\\t] here the ^ means "not a")
  • followed by tab
  • followed by 3
  • followed by tab

Now we have effectively expressed the idea that we need a 3 as the content of the second column ( \\t3\\t ) and we are not interested in the precise content of the first column. The ^[^\\t]*\\t is only necessary to express the idea "what follows is in the second column".

If you want to match something in the fourth column, you could use this to "skip" the first three column and match a 4 in the fourth column: ^([^\\t]*\\t){3}4 . (Note the parenthesis and the {3} ).

As you can see many details and awk is much more elegant and easy.

You can read this up in the documentation of grep and then you will need to study something about regular expression, eg start here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM