I'm wondering how to improve my grep use when being confronted with files with very long lines. Imagine something like "packed" JavaScript files that only have 1 line. Sometimes files like that spit out results which I didn't expect and flood my console (especially when using --recursive).

I guess I would like something like --context but for words/bytes around the match. I thought I was being smart by coming up with this usage:

$ grep -rino --color ".\{0,20\}something.\{0,20\}" *

But that makes the characters around my search term part of the match.


I guess a solution was somewhat in front of my face. I'm now using this function in my .bashrc:

  grep -Erino ".{0,20}$1.{0,20}" ${@:2:$(($#-1))} | grep -i --color "$1"
alias g=grepAdjusted

I kept it simple and to my most common use case. It's not a perfect answer to my original question, but it provides what I was looking for.

Output of grepAdjusted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.