Grep characters before and after match?

前端 未结 7 1503
长情又很酷
长情又很酷 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条回答
  •  遥遥无期
    2020-12-02 04:47

    You mean, like this:

    grep -o '.\{0,20\}test_pattern.\{0,20\}' file
    

    ?

    That will print up to twenty characters on either side of test_pattern. The \{0,20\} notation is like *, but specifies zero to twenty repetitions instead of zero or more.The -o says to show only the match itself, rather than the entire line.

提交回复
热议问题