comment/delete a particular pattern starting from second line of the matching pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comment/delete a particular pattern starting from second line of the matching pattern
# 1  
Old 10-12-2008
comment/delete a particular pattern starting from second line of the matching pattern

Hi,

I have file 1.txt with following entries as shown:

0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
**
**
**

In file 2.txt I have the following entries as shown:

0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
0789456|332211|10.20.30.40|078945633
1234567|225522|10.20.30.50|123456733
0321654|999999|10.20.30.40|032165433
0456123|777899|10.20.30.40|045612333
***
***
***

I want take the IP Address column from 1.txt and then search it in 2.txt and comment the duplicate entry from second matched pattern.

i.e., i need to have the following output in 2.txt as shown:

0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
#0789456|332211|10.20.30.40|078945633
#1234567|225522|10.20.30.50|123456733
#0321654|999999|10.20.30.40|032165433
#0456123|777899|10.20.30.40|045612333
***
***
***

Below is the script i wrote but it will comment all the duplicate entries:

for i in `cat 1.txt |cut -d"|" -f3`
do
cat 2.txt |sed "s/^\(.*\|$i\|.*)/#/g" > tmp.txt
mv tmp.txt 2.txt
done

Can some one guide me how to leave the first duplicate entry untouched and comment/delete from second duplicate entry and so on.
Smilie
Please do not close this thread if you do not know the answer.

Thanks
-Imas
# 2  
Old 10-12-2008
Quote:
Originally Posted by imas
Please do not close this thread if you do not know the answer.
Is this a home work question?
Assuming that it's not, what is the real world problem of your question?

Regards
# 3  
Old 10-13-2008
Yes your assumption is correct and this is a real world problem

What iam trying to do is commenting the duplicate Ip's found in the text file ignoring the first occurance as to be valid IP for a valid mac and from the second occurance onwards commenting the duplicate IPs at the starting of the line.

file 1.txt with following entries as shown:

0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
**
**
**

file 2.txt with following entries as shown:

0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
0789456|332211|10.20.30.40|078945633
1234567|225522|10.20.30.50|123456733
0321654|999999|10.20.30.40|032165433
0456123|777899|10.20.30.40|045612333
***
***
***

i need to have the following output in 2.txt as shown:

0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
#0789456|332211|10.20.30.40|078945633
#1234567|225522|10.20.30.50|123456733
#0321654|999999|10.20.30.40|032165433
#0456123|777899|10.20.30.40|045612333
***
***
***

The correct script i wrote is

for i in `cat 1.txt |cut -d"|" -f3`
do
cat 2.txt |sed "s/^\(.*\|$i\|.*)/#\1/g" > tmp.txt
mv tmp.txt 2.txt
done

But the above script will comment all the occurance.

Your help is appreciated.

Thanks
-imas
# 4  
Old 10-13-2008
awk:

Code:
nawk 'BEGIN{FS="|"}
{
if(NR==FNR)
	a[$3]=0
else
{
	a[$3]++
	if(a[$3]>=2)
		print "#"$0
	else
		print $0
}
}' 1.txt 2.txt

# 5  
Old 10-13-2008
Hi Summer_Cherry,

Million thanks for giving me the piece of code which will comment starting from second occurance of matched pattern from the starting of line.

intelsol2>cat 1.txt
0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
intelsol2>cat 2.txt
0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
0789456|332211|10.20.30.40|078945633
1234567|225522|10.20.30.50|123456733
0321654|999999|10.20.30.40|032165433
0456123|777899|10.20.30.40|045612333
intelsol2>
nawk 'BEGIN{FS="|"}
{
if (NR==FNR)
a[$3]=0
else
{
a[$3]++
if (a[$3]>=2)
print "#"$0
else
print $0
}
}' 1.txt 2.txt >3.txt
intelsol2>cat 3.txt
0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
#0789456|332211|10.20.30.40|078945633
#1234567|225522|10.20.30.50|123456733
#0321654|999999|10.20.30.40|032165433
#0456123|777899|10.20.30.40|045612333
intelsol2>

Also thanks a lot for unix forum guys "you ppl rock!!!"

FYI, i was trying to search for this code since all the weekend and went through sed manual "http://www.grymoire.com/Unix/Sed.html" sub topic (/1, /2, etc. Specifying which occurrence) however was unlucky in trying all the trial and error method.

Also Summer could you please explain me the code you have written so that i can understand it in a better way.

Again thanks a lot and you can close this thread.

Thanks
-imas
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep file starting from pattern matching line

I have a file with a list of references towards the end and want to apply a grep for some string. text .... @unnumbered References @sp 1 @paragraphindent 0 2017. @strong{Chalenski, D.A.}; Wang, K.; Tatanova, Maria; Lopez, Jorge L.; Hatchell, P.; Dutta, P.; @strong{Small airgun... (1 Reply)
Discussion started by: kristinu
1 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. Shell Programming and Scripting

Delete multiple lines starting with a specific pattern

Hi, just tried some script, awk, sed for the last 2 hours and now need help. Let's say I have a huge file of 800,000 lines like this : It's a tedious job to look through it, I'd like to remove those useless lines in it as there's a few thousands : Or to be even more precise : if line1 =... (6 Replies)
Discussion started by: Zurd
6 Replies

5. Shell Programming and Scripting

delete lines starting with a pattern

i have a file sample.txt containing i want to delete lines starting with 123 neglecting spaces and tabs. but not lines containing 123. i.e. i want files sample.txt as help me thanxx (4 Replies)
Discussion started by: yashwantkumar
4 Replies

6. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

7. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies

8. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

10. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies
Login or Register to Ask a Question