DEV Community

Kenneth Lum
Kenneth Lum

Posted on

4 2

To grep for a word and its adjacent characters

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.

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more