Re: Inserting Text In Specific Location Using Shell Script



Well, there are two ways that come to my mind to do this.

First is to use sed (or awk) to cut the first X number of lines up to and including comment 2 and save as like file1_head.txt and cut the bottem lines after comment 2 and save that as file1_tail.txt. You could even use 'head' and 'tail' for this if you know the specific line number. Then you can just do cat file_to_add_in_middle.txt >> file1_head.txt and then cat file1_tail.txt >> file1_head.txt and then mv file1_head.txt file1.txt That seems kind of backwards but it should work

The other way you could do this is using tr. This is knid of dumb but you could to tr "#comment 2" "#comment 2 these lines are the new ones" and it will add that. Although that wil probably mess up the formatting, you might be able to use like /n or something in tr to say you want a new line. good luck

Nate Mayotte

---- Cesar Covarrubias <cesar@xxxxxxx> wrote:
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

--
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



Relevant Pages