LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-19-2008, 11:10 AM   #1
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
Question Sed append text to end of line if line contains specific text? How can this be done?


Hi All,
I'm trying to get sed to add some text to the end of a line if that line contains a specific piece of text.

Quite simple i have a script...
echo blah
df -h
echo blah1
cd /usr/share
echo blah2
ls -al
echo blah1

And i want to use sed (i think is probably right for the job) to add text to the end of all the lines that are 'echo'

I've seen that you can :-
sed 's/$/text/' filename

But that appends to every single line i want to do something like
sed 's|echo*$|>> /var/log/output_to_log.txt|' filename

As you can see i want all the echo's to send out to a log, in the script i've already created but at the moment the echo's just send to stout, and i could run the script with filename 2>&1 /var/log/output_to_log.txt, but i'm trying to do it this other way.

Any ideas?
Cheers,
MJ

Last edited by helptonewbie; 11-19-2008 at 11:28 AM.
 
Old 11-19-2008, 11:37 AM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

This works for me:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate append text to end of selected lines.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed
set -o nounset

echo
echo " Results:"
cat <<EOF |
echo blah
df -h
echo blah1
cd /usr/share
echo blah2
ls -al
echo blah1
EOF
sed '/^echo/s|$| >> /var/log/output_to_log.txt|'

exit 0
Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
GNU sed version 4.1.2

 Results:
echo blah >> /var/log/output_to_log.txt
df -h
echo blah1 >> /var/log/output_to_log.txt
cd /usr/share
echo blah2 >> /var/log/output_to_log.txt
ls -al
echo blah1 >> /var/log/output_to_log.txt
The page http://www.grymoire.com/Unix/Sed.html is often mentioned as useful for learning sed ... cheers, makyo
 
Old 11-19-2008, 11:43 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Deleted as makyo's example:

sed -i '/echo/s|$| >> /var/log/output_to_log.txt|' filename

is more relevant and elegant.

Last edited by Disillusionist; 11-19-2008 at 11:50 AM.
 
Old 11-19-2008, 01:26 PM   #4
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
Sweet thanks guys both ideas work great. And great website not seen that one before.. cheers
MJ
 
Old 10-23-2013, 01:48 PM   #5
jheengut
Member
 
Registered: Sep 2006
Location: Providence, Moka Mauritius
Distribution: Slackware, OpenSuse, Debian, Fedora, Mageia, Ubuntu, RedHat, BSD, Gentoo, Puppy
Posts: 181
Blog Entries: 2

Rep: Reputation: 2
so one liner

sed -e " /^http/s|$|\'| " -e "s/^http/\'http/"

sed -e " /^http/s|$|\'| ; s/^http/\'http/"

err to one liner ( better ) without two -e please


from

http://foo.foo.com

to

'http://foo.foo.com'

Last edited by jheengut; 10-23-2013 at 01:53 PM. Reason: searched o+n
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
append a line to text file in script roadrash Linux - Newbie 4 10-01-2008 09:03 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM


All times are GMT -5. The time now is 01:36 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration