About 'grep -f': match pattern with file

前端 未结 2 747
醉酒成梦
醉酒成梦 2021-02-15 13:26

I am using the grep -f function to extract lines from a file which match a particular pattern. Let\'s say my pattern file is pattern.txt, as follows.

2条回答
  •  情书的邮戳
    2021-02-15 13:48

    Well, there are lots of possible solutions. You are telling grep to find all lines containing a 3, and the line '8::3rr' does in fact contain a 3. so you need to be more specific in what you're looking for.

    For example, you can change the patterns to '1:', '2:', etc. to match only numbers followed by a colon. Or you could change them to '^1', '^2', etc. to match only numbers at the beginning of the line. It depends on your data, but probably you want both, so that your search for '1:' doesn't match '21:' and your search for '^5' doesn't match '53:'. In which case your patterns file would like like this:

    ^1:
    ^2:
    ^3:
    ^4:
    ^5:
    

提交回复
热议问题