LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep command and adding a line (https://www.linuxquestions.org/questions/linux-newbie-8/grep-command-and-adding-a-line-737259/)

Treikayan 07-02-2009 02:17 PM

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


dorian33 07-02-2009 02:19 PM

use sed

Treikayan 07-02-2009 09:15 PM

Quote:

Originally Posted by dorian33 (Post 3594633)
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>

dorian33 07-03-2009 02:30 AM

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

Treikayan 07-03-2009 09:57 AM

Quote:

Originally Posted by dorian33 (Post 3595227)
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


All times are GMT -5. The time now is 11:32 AM.