Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive line append with sed 3

Status
Not open for further replies.

UsRb

IS-IT--Management
Mar 19, 2007
48
HR
Hi2all,

I have file 1 which contents are:

#PTEDFLE 10.228.101.180
#PTEDFL8 10.228.101.180
#PTEDFLB 10.228.101.180
#PTEDFLF 10.228.101.176
#PTEDFLP 10.228.101.154
#PTEDFLN 10.228.101.154
#PTEDFLM 10.228.101.154

and other file 2 which holds data from first column of file 1.

I want to append with ip address (2nd column of file 1) every line in file 2 which contain column 1 in file 1.

This doesn't work:

while read term ip
do sed '/$term/ a\ $ip' 2 > test
done < 1

But if I try with appending only one value like this, it works:

sed '/#PTEDFLE/ a\ 10.228.101.180 2 >test

What am I missing??
 
Maybe this ?
Code:
do sed [!]"[/!]/$term/ a\ $ip[!]"[/!] 2 > test

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
And as you're in a loop, test will contain only the last modification.
What about something like this ?
Code:
cp file2 test
while read term ip
do sed "/$term/a\ $ip" test > test.$$ && mv test.$$ test
done < file1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Last post does the job, thank you!
 
I noticed that it does not append a line but creates new line:

Oneliner
10.228.101.180

Why does it not append like this:

Oneliner 10.228.101.180

 
Replace this:
sed "/$term/a\ $ip"
with this:
sed "/$term/s!\$! $ip!"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Unfortunately, this is the output :-( :

-bash: !\$!: event not found
 
Sorry for interrupting PHV ;-)

For bash you have to use a different character instead of ! in the sed command as it has a special meaning in bash history processing; And in this case the !'s are not hidden from bash by single quotes because you need to expand shell variables...

Try this:

[tt]sed "/$term/s:\$: $ip:"[/tt]



HTH,

p5wizard
 
Works like a charm :) You deserve a star!
 
Feherke, I'm not a basher by nature, so I did not know that...
But I learned something new today - have a *


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top