Inserting Text In Specific Location Using Shell Script
Michael Velez
mikev777 at hotmail.com
Fri May 26 20:10:08 UTC 2006
> -----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
More information about the redhat-list
mailing list