script to delete a line in a file based on a pattern

dhuma
dhuma used Ask the Experts™
on
I have a requirement to compare the file contents in two different directories..

Dir One
Test.java
Abc.java

Dir two
Test.java
Abc.java


before comparing the files for differences, I would like to delete few lines in these files,
for example, these files have CVS repository information on the top

 Source:         $Source: /home/test/blah$
 Module           $abc$

can some one provide a script to delete these lines based on a given pattern in all the files in a directory.

Once those lines are deleted, I will use a dircmp command to perform the diff on the directories..

thanks

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
#!/bin/bash

for F in `find . -type f`
do
   cd `dirname $F`
   FILE=`basenmae $F`
   awk ' !/^Source:/ || !/^Module/ {print}' $FILE > ${FILE}_new
   /bin/mv -f ${FILE}_new $FILE
done
Top Expert 2011

Commented:
Some fixes

#!/bin/bash

for F in `find /full-path-to-top-dir  -type f`
do
   cd `dirname $F`
   FILE=`basenmae $F`
   awk ' !/^Source:/ || !/^Module/ {print}' $FILE > ${FILE}_new
   /bin/mv -f ${FILE}_new $FILE
done

Where say your files are under /home/cvs/work, then replace /full-path-to-top-dir to /home/cvs/work

Author

Commented:
Chen
thanks for the quick response, however, the awk command is not replacing the lines that are starting with Module or Source
i have this file

Test.txt
Line 1
Line 2
Module
Source

I run the following
FILE=Test.txt
awk ' !/^Source:/ || !/^Module/ {print}' $FILE

it doesnt seem to be deleteing those lines, can you cross check?
Top Expert 2011
Commented:
Woo, logical mistake.
So be:
awk ' !(/^Source/ || /^Module/)  {print}' test.txt

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial