unable to insert string on particular lines using sed
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sed '10,15 <command>'
means do the command over the range 10-15. In you case, the command is "i" for insert. Thus you wind up with the inserted text on every other line. (Specifically, "i" inserts a new line in place of the one specified--and then moves everything down.)
In my docs, I don't see how to do what you want without simply having two commands, eg:
sed -e '10 i <text>' -e '15 i <text>' filename
The addresses and <text> can both be variables. (Use double-quotes if you have a "$" in the sed expression---or escape it.)
thanks for the info.....I was being somewhat lazy to read all these documentation ......got the point now...I will go through these tutorials again and bug you people again if I got a doubt.
To avoid typing in the text repeatedly you can do this:
Code:
sed '3ba; 5ba; 7ba; b; :a; iboogie'
~$ for ((i=1; i<=10; i++)) ; do echo $i ; done | sed '3ba; 5ba; 7ba; b; :a; iboogie'
1
2
boogie
3
4
boogie
5
6
boogie
7
8
9
10
~$
Basically, I've set up a mark at the end of the code (:a) with an instruction after it (insert boogie). I prevent the instruction from being executed by putting a b command before it (jump to the end of the script). The only way I can get to the code is by explicitly jumping to the mark, which I do on lines 3, 5 and 7.
Thanks a lot Feder...It is working however, I had to modify it to insert on the desired lines...also the explaination is very helpful to understand the conecpt ....Thanks again...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.