Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ?

Thank you!

share|improve this question
up vote 10 down vote accepted

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}$'
share|improve this answer
+1 ack-grep on some systems – Dennis Williamson Jul 14 '12 at 11:38
Also, ack will ignore minified Javascript files. It sees them as not being source code, and so ignores. – Andy Lester Jul 23 '12 at 14:07
My grep only worked with {0,100} for some reason. – temoto Jan 30 '13 at 12:34

Your Answer


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.