Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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