How to set line length limit when using grep (Filter result by line length)?

The use case is that I am searching for a particular CSS attribute in a directory of HTML/CSS/JS files (including sub-directories). Some one-liner JS libraries also have the CSS attribute that I'm searching for.

So is there a way to tell grep that it should not give result on a line which is longer than my defined $max_line_length?


First approach, try to exclude the one-liner JS files from being grepped in the first place. Often these will have a name like some-library.min.js, so you could do something like:

$ grep --exclude '*.min.js' ...

Another approach is that if you know that it's going to be in a CSS file, you can use ack to restrict your search to CSS files (and ignore various VCS cruft):

$ ack --type=css ...

However, to answer your question, you can write a regular expression to match on line length. The following will match any line with 100 or fewer characters.

$ grep -E '^.{,100}$'

I prefer to pipe through cut to cut the lines amd invert match -v to exclude stuff. Better readable to me. I guess perfomance wise not optimal

grep -r something . | cut -b 1-99 | grep -v min.js