Exclude files that have very long lines of text from grep output

grep

I often run grep commands to find things in my code, but the problem with web projects is that there will often be compressed JavaScript and CSS files which create one huge line of text, so that if a match is found, the whole terminal window is filled for more then a 1000 lines, making it extremely impractical to find what I'm looking for.

So is there a way to avoid files that have say single lines of text over 200 characters?

Best Answer

With GNU grep and xargs:

grep -rLZE '.{200}' . | xargs -r0 grep pattern

Alternatively, you could cut the output of grep:

grep -r pattern . | cut -c1-"$COLUMNS"

or tell your terminal not to wrap text if it supports it:

tput rmam
grep -r pattern .

or use less -S

grep -r pattern . | less -S
Related Question