4

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.

Oliver Salzburg
  • 87,539
  • 63
  • 263
  • 308

1 Answers1

1

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

grepAdjusted()
{
  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

Oliver Salzburg
  • 87,539
  • 63
  • 263
  • 308