DEV Community

loading...

To grep for a word and its adjacent characters

kennethlum profile image Kenneth Lum ・1 min read

Sometimes we may want to grep for some words, and because the line is too long, sometimes even the whole file on one single line if it is a generated file, we may only want to see some characters before and after the match.

In that case, we can use something like:

grep -o -E '.{30}theword.{30}' somefile.html
Enter fullscreen mode Exit fullscreen mode

or

curl https://somewebsite.com | grep -o -E '.{30}theword.{30}'
Enter fullscreen mode Exit fullscreen mode

The -E says to treat it like it is egrep, and -o means only output the matched characters but not the whole line.

Discussion (0)

pic
Editor guide