find and grep

It seems I’m using this kind of thing a lot:

find . -name '*.jsp' -exec grep -q 'contentType='  '{}' \; -print

This example finds all jsp files that contain the text ‘contentType=’. Actually I’m more interested in the ones that don’t, so I’d add a -v flag to the grep.

Update: oops, the latter doesn’t actually work – it finds jsp files which contain a line that doesn’t contain ‘contentType=’, which is all of them. I’ll have to think about this some more.

Update more: Ok, the way to get what I want is to make find apply the ‘not’ logic, like this:

find . -name '*.jsp' ! -exec grep -q 'contentType='  '{}' \; -print

Next I want to take this list of files, and insert the header with the contentType at the top of each one. I put my header text in a file called ‘pageheader’, and used this command with ed, which seemed to do the trick:

for f in $(find . -name "*.jsp" ! -exec grep -q 'contentType=' '{}' \; -print); do ed -s "$f" <<< $'0r pageheader\nw'; done

Post a Comment

Your email is never published nor shared. Required fields are marked *