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 |
use sed
|
Quote:
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> |
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 |
Quote:
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. |