Avatar of dhuma
dhuma
 asked on

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

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

Unix OS

Avatar of undefined
Last Comment
dhuma

8/22/2022 - Mon
wesly_chen

#!/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
wesly_chen

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
dhuma

ASKER
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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
wesly_chen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dhuma

ASKER
thanks