Inserting Text In Specific Location Using Shell Script

Cesar Covarrubias cesar at uci.edu
Mon May 29 01:01:26 UTC 2006


Hello,

Thanks to all who replied. Michael's suggestion was the easiest to
implement, and it worked well. 

Very Respectfully,
Cesar Covarrubias
-----Original Message-----
From: redhat-list-bounces at redhat.com [mailto:redhat-list-bounces at redhat.com]
On Behalf Of Michael Velez
Sent: Friday, May 26, 2006 1:10 PM
To: 'General Red Hat Linux discussion list'
Subject: RE: Inserting Text In Specific Location Using Shell Script

 

> -----Original Message-----
> From: redhat-list-bounces at redhat.com 
> [mailto:redhat-list-bounces at redhat.com] On Behalf Of Cesar Covarrubias
> Sent: Friday, May 26, 2006 3:51 PM
> To: General Red Hat Linux discussion list
> Subject: Inserting Text In Specific Location Using Shell Script
> 
> Hello,
> 
> I am running a script on about 30 machines and need to insert 
> text after a specific comment. For example:
> 
> #comment 1
> 
> #comment 2
> 
> #comment 3
> 
> I want to add text after #comment 2, on a new line. I have 
> been trying to work with both sed and awk but no success. 
> This has to be done in bash or sh and cannot be done in any 
> other language. Any ideas?
> --
> Very Respectfully,
> Cesar Covarrubias
> cesar at uci.edu
> 

You can do this with sed.  The trick is to use the 's' command in sed and
actually add a real newline in the substitution field.  But the newline
needs to be preceded by a '\'.  The script then contains two lines.

Here is a sample sed script which will add a line '#Addendum to comment 2'
right below '#comment 2':


s/#comment 2/#comment 2\
#Addendum to comment 2/



The output of this script executed on the example you gave us is:


#comment 1

#comment 2
#Addendum to Comment2

#comment 3


Hope this helps,
Michael

-- 
redhat-list mailing list
unsubscribe mailto:redhat-list-request at redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/redhat-list




More information about the redhat-list mailing list