Friday 4 May 2018

Get additional data around match from grep


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.



Answer



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


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...