LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2009, 04:06 AM   #1
usrb
LQ Newbie
 
Registered: Oct 2008
Posts: 7

Rep: Reputation: 0
Recursive line append with sed


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??
 
Old 04-01-2009, 04:31 AM   #2
khodeir
Member
 
Registered: Feb 2009
Distribution: Debian
Posts: 243

Rep: Reputation: 33
the problem may be in this line
Quote:
sed '/'$term'/ a\ '$ip'' 2> test
 
Old 04-01-2009, 04:35 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
show your file2, and show how your output will be like
 
Old 04-01-2009, 05:10 AM   #4
usrb
LQ Newbie
 
Registered: Oct 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Ok, I figured it out, this is the solution:

cp 2 test
while read term ip;
do sed "/$term/ a\ $ip" test > test.$$ && mv test.$$ test;
done < 1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
using sed to append a variable after a certain line jadeddog Programming 6 11-05-2008 12:49 AM
sed script to append variable text gmartin Linux - General 4 12-27-2006 04:44 PM
Trying to use sed to append C opening /* and close */ to code deoren Linux - Newbie 5 10-10-2003 05:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration