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

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

Option 1: You can exclude files matching a certain pattern:

grep --exclude='*.min.*'

This will exclude script.min.js and style.min.css... Other grep option include --exclude-from=FILE and --exclude-dir=DIR

Option 2: I am not sure if this is practical, but you can cut the first 200 chars of each line, and then grep them:

grep -H [OPTIONS] PATTERN [FILE...] | cut -c1-200 | grep PATTERN

The first grep does an initial match and output the file name and the line, the second one ensures the PATTERN is still there after cutting the lines.


In this kind of situation, I like to grep a pattern with a neighborhood context (lets say 30 chars):

grep -Po '.{0,30}pattern.{0,30}' *.js

Tags:

Grep