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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a 'sed' problem

Status
Not open for further replies.

g2345c

Technical User
Jun 23, 2004
22
US
Question: How do I append text in the middle of a line in a text file? Let say, I have file foo.txt

>> echo foo.txt
line1.This.is;a.test /var/messages
line2.This is #a test


Now I would like to search in the file foo.txt for a string that contain "line1". Then append string "GOT.IT" to this line so it will look like this:
line1.This.is;a.testGOT.IT /var/messages

Basically I want to append "GOT.IT" to the first white-space on that line

It can be done with either sed or awk ...
thanks in advance
 
Not tested, but should be close to what you want

sed -n '/line1/s/ /GOT.IT /p' foo.txt

CaKiwi
 
Or if you want all the unchanged lines output as well

sed '/line1/s/ /GOT.IT /' foo.txt

CaKiwi
 
thanks for your help but it doesn't work I try all 2.

here is the content of foo.txt:
*.info;mail.none;authpriv. /var/log/messages

# The authpriv file has restricted access.
authpriv.* /var/log/secure

here is my script:
#!/bin/sh
#sed '/info/s/ /GOT.IT /' 123
sed -n '/info/s/ /GOT.IT /p' 123



What wrong?
Thanks a lot

 
You have 123 instead of foo.txt in your sed statements

CaKiwi
 
Sorry, it should be foo.txt ... Typo.

thanks
 
Here's my results

sed '/info/s/ /GOT.IT /' foo.txt

*.info;mail.none;authpriv.GOT.IT /var/log/messages

# The authpriv file has restricted access.
authpriv.* /var/log/secure

CaKiwi
 
Hi, Please help me why it doesn't work for me. Thanks


linuxbuild : /home/g2345c >sed '/info/s/ /GOT.IT /' foo.txt
info;mail.none;authpriv.none;cron.none /var/log/messages

# The authpriv file has restricted access.
authpriv.* /var/log/secure
linuxbuild : /home/g2345c >
 
Perhaps there is a tab in the file and not a space. Enter

od -c foo.txt

to find out.

CaKiwi
 
Wow... Cool it's a tab not a space. So how do you replace a tab. I try \t but it doesnt wotk. it replaces the t instead of a tab. Sorry, I am a newbie and is learing this stuff. Thanks a lot

sed '/info;mail/s/\t/GOT.IT /' foo.txt
info;mail.none;auGOT.IT hpriv.none;cron.none /var/log/messages

# The authpriv file has restricted access.
authpriv.* /var/log/secure
 
it doesn't work for me
Any error message ? Unexpected behaviour ?
Can you please post your script, the output produced and the expected output relative to the input you already posted.
I guess you forgot to get rid of the -n option for sed

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Just type a tab instead of a space. You might also want to put a tab in the replacement text as well.

CaKiwi
 
Thank you very much ... It's worked :) ...
How do I tell sed to to save the foo.txt file after replace the first tab with GOT.IT?

Thanks
 
g2345c,

sed '/info;mail/s/ /GOT.IT /' foo.txt > foo.tmp && mv foo.tmp foo.txt

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top