I have a find/grep command that i use to search my projects' codebase, thus:

find folder1 folder2 folderetc -print | awk '{print "\""$0"\""}' | xargs grep -n "search-string"

Something that irritates me regularly is minified javascript & css files, which have everything packed into a single line (due to a probably misguided belief that removing newline characters will make downloading javascript files significantly quicker). With these packed or minified files, there is always a non-packed version, which i want to see in my results. I never want to see the packed version, as it just results in screens and screens of unreadable garbage.

There's no systematic way that i can tell grep to avoid minified or packed files, as there's no naming convention. But it occurred to me that i could filter out results where the line is more than, say, 400 characters long. Is there a way i can do this with grep (or something else?)

  • 2
    Also, take a look at ack (or ack-grep). You can include a regex that specifies length and it will do the work of find and xargs for you. Plus it can be made to only search files of specified types. – Paused until further notice. Apr 15 '14 at 11:19

Yes, there is:

$ echo foobar | awk 'length($0) < 7 && $0~/foo/'

Replace the echo with your find command and foo with the string to search for. Adjust the 7 to the length you want to filter by.

  • Thanks @dawud - i ended up keeping the search term via grep because i like the way it displays the line number, but tagging the awk onto the end: find folder1 folder2 folderetc -print | awk '{print "\""$0"\""}' |xargs grep -n "search-term" | awk 'length($0) < 400' – Max Williams Apr 15 '14 at 11:13

You may want to take a look at ack, which is basically grep optimized for programmers. It ignores minified JS and CSS files, as well as any version control system files lying around in your directory.

  • @DennisWilliamson already suggested this in an earlier comment, too. I've still not got round to looking at ack, but thanks for the prod. – Max Williams May 7 '14 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.