Grep characters before and after match?

前端 未结 7 1508
长情又很酷
长情又很酷 2020-12-02 04:19

Using this:

grep -A1 -B1 \"test_pattern\" file

will produce one line before and after the matched pattern in the file. Is there a way to di

7条回答
  •  Happy的楠姐
    2020-12-02 05:08

    grep -E -o ".{0,5}test_pattern.{0,5}" test.txt 
    

    This will match up to 5 characters before and after your pattern. The -o switch tells grep to only show the match and -E to use an extended regular expression. Make sure to put the quotes around your expression, else it might be interpreted by the shell.

提交回复
热议问题