Operating System - HP-UX
1752810 Members
5972 Online
108789 Solutions
New Discussion юеВ

Re: Match Pattern and delete lines

 
SOLVED
Go to solution
Rahul_13
Advisor

Match Pattern and delete lines

Hi,

I have 2 files one containing some patterns and the other file with lines having the pattern.

I need to read both the files and delete all the lines in file2 which matches the patterns in file1.

Thanks,
Rahul
4 REPLIES 4
Sundar_7
Honored Contributor
Solution

Re: Match Pattern and delete lines

file1 = file with the patterns
file2 = file with lines having the pattern

grep -vf file1 file2 > file3
mv file3 file2
Learn What to do ,How to do and more importantly When to do ?
Steven E. Protter
Exalted Contributor

Re: Match Pattern and delete lines

while read -r file1d
do
while read -r file2d
do
hit=0
hit=$(echo $file2d | grep $file1d)
if [ $hit -eq 0
echo $file2d >> /tmp/newfile2
else
echo "data reject
fi
done < /tmp/file2
done < /tmp/file1

Needs a tweak or two, but this approach might work.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Prashant Zanwar_4
Respected Contributor

Re: Match Pattern and delete lines

I believe this is something additional to your yesterday's question.
you will have to use sed for this.

cat file1 | while read pattern
do
grep -v $pattern file2 >> /tmp/file3
mv file2 file2.old
mv file3 file2
done

Hope this helps
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."
Prashant Zanwar_4
Respected Contributor

Re: Match Pattern and delete lines

While it is multiple pattern, you would like to do like following

cat file 1| while read pat1 pat2 pat3 ..
do
egrep -v "$pat1|$pat2|...$patn" file2 >> file3
..
..
..

Hope it helps
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."