LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-02-2009, 02:17 PM   #1
Treikayan
Member
 
Registered: Oct 2008
Location: Albany Park, Chicago IL
Distribution: RHEL 5.1 i386
Posts: 75

Rep: Reputation: 15
Grep command and adding a line


Hello,

I have several configs in a file that need a line of text added to each config. How would I execute a command to add the line of text after a certain pattern is found in the file.

I tried this, but it doesn't work:

Code:
cat sipt-ae.cfg.test | grep -v 'host =' sipt-ae.cfg.test | printf "%b" '\nmenuextra = <a target='_blank' href='tr/tr.html{HOST}' class='{CLASS}' onclick="window.open(this.href,this.target, 'width=800,height=500,toolbar=no,location=no,status=no,scrollbars=no'); return false;">*</a>' > test.txt
[root@dalimnag02 include]# less test.txt
 
Old 07-02-2009, 02:19 PM   #2
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
use sed
 
Old 07-02-2009, 09:15 PM   #3
Treikayan
Member
 
Registered: Oct 2008
Location: Albany Park, Chicago IL
Distribution: RHEL 5.1 i386
Posts: 75

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by dorian33 View Post
use sed
I can't get this to work.

I want to add a new line for each config after the line that has "alerts=" listed in text.txt:

menuextra = <a target='_blank' href='tr/tr.html{HOST}' class='{CLASS}' onclick="window.open(this.href,this.target, 'width=800,height=500,toolbar=no,location=no,status=no,scrollbars=no'); return false;">*</a>

test.txt

config 1
alerts=information

config 2
alerts=more information

config 3
alerts=some more information

after modifications (test.txt):

config 1
alerts=information
menuextra = <a target='_blank' href='tr/tr.html{HOST}' class='{CLASS}' onclick="window.open(this.href,this.target, 'width=800,height=500,toolbar=no,location=no,status=no,scrollbars=no'); return false;">*</a>


config 2
alerts=more information
menuextra = <a target='_blank' href='tr/tr.html{HOST}' class='{CLASS}' onclick="window.open(this.href,this.target, 'width=800,height=500,toolbar=no,location=no,status=no,scrollbars=no'); return false;">*</a>


config 3
alerts=some more information
menuextra = <a target='_blank' href='tr/tr.html{HOST}' class='{CLASS}' onclick="window.open(this.href,this.target, 'width=800,height=500,toolbar=no,location=no,status=no,scrollbars=no'); return false;">*</a>
 
Old 07-03-2009, 02:30 AM   #4
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
Code:
sed -re 's|(^alerts=.*$)|\1\nmenuextra = <a target='_blank' href='tr/tr.html{HOST}' class='{CLASS}' onclick="window.open(this.href,this.target, 'width=800,height=500,toolbar=no,location=no,status=no,scrollbars=no'); return false;">*</a>\n|' test.txt
 
Old 07-03-2009, 09:57 AM   #5
Treikayan
Member
 
Registered: Oct 2008
Location: Albany Park, Chicago IL
Distribution: RHEL 5.1 i386
Posts: 75

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by dorian33 View Post
Code:
sed -re 's|(^alerts=.*$)|\1\nmenuextra = <a target='_blank' href='tr/tr.html{HOST}' class='{CLASS}' onclick="window.open(this.href,this.target, 'width=800,height=500,toolbar=no,location=no,status=no,scrollbars=no'); return false;">*</a>\n|' test.txt
Thank you.

It works beautifully. I kept trying the command with the slashes using s/ and /a.

sed -e 's/^alerts=.*$/\nmenuextra = <a target='_blank' href='tr/tr.html{HOST}' class='{CLASS}' onclick="window.open(this.href,this.target, 'width=800,height=500,toolbar=no,location=no,status=no,scrollbars=no'); return false;">*</a>/a' test.txt
 
  


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
LXer: Using Grep To Streamline Your Shell And Command Line Scripting LXer Syndicated Linux News 0 08-08-2008 07:11 AM
Help me in Grep Command + cd command in single line JeiPrakash Linux - Newbie 3 05-27-2008 04:16 AM
Adding values on command line.. 3saul Linux - Software 1 03-06-2006 03:01 AM
Command line tool to find(1) or grep(1) faster? rsheridan6 Linux - Software 2 02-18-2006 03:50 PM
Adding users via command Line AmdMhz Linux - Software 4 10-31-2004 01:29 PM


All times are GMT -5. The time now is 07:19 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration