RE: Inserting Text In Specific Location Using Shell Script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx [mailto:redhat-list-bounces@xxxxxxxxxx]
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@xxxxxxxxxx 
> [mailto:redhat-list-bounces@xxxxxxxxxx] 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@xxxxxxx
> 

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@xxxxxxxxxx?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/redhat-list

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

[Index of Archives]     [CentOS]     [Kernel Development]     [PAM]     [Fedora Users]     [Red Hat Development]     [Big List of Linux Books]     [Linux Admin]     [Gimp]     [Asterisk PBX]     [Yosemite News]     [Red Hat Crash Utility]


  Powered by Linux