Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This 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!


  Search this Thread
Old 03-29-2008, 06:55 AM   #1
Registered: Jun 2006
Location: India
Distribution: RHEL,Suse,Fedora
Posts: 106

Rep: Reputation: 15
unable to insert string on particular lines using sed

Hi all,

I want to insert "headlines" at 10th, and 15th line number of test_file using sed in one command. I tried following.

sed '10,15iheadlines" test_file

it inserted on 10 but also on 12,14,16,18,20 ....don't know why
Is it possible to inert a string on a number of lines using sed ?
Old 03-29-2008, 07:45 AM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.)

Good SED tutorial here:
Old 03-29-2008, 08:12 AM   #3
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Seder's grab bag:
Old 03-29-2008, 08:37 AM   #4
Registered: Jun 2006
Location: India
Distribution: RHEL,Suse,Fedora
Posts: 106

Original Poster
Rep: Reputation: 15
thanks for the info.....I was being somewhat lazy to read all these documentation the point now...I will go through these tutorials again and bug you people again if I got a doubt.

thanks again......
Old 03-30-2008, 07:45 AM   #5
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
Originally Posted by pixellany View Post
sed '10,15 <command>'

sed -e '10 i <text>' -e '15 i <text>' filename
To avoid typing in the text repeatedly you can do this:

sed '3ba; 5ba; 7ba; b; :a; iboogie'

~$ for ((i=1; i<=10; i++)) ; do echo $i ; done  | sed '3ba; 5ba; 7ba; b; :a; iboogie'
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.

Regards, Feder
Old 03-30-2008, 11:28 PM   #6
Registered: Jun 2006
Location: India
Distribution: RHEL,Suse,Fedora
Posts: 106

Original Poster
Rep: Reputation: 15
Thumbs up

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

Last edited by livetoday; 03-30-2008 at 11:34 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
using sed to insert lines with special characters disorderly Linux - Software 26 04-20-2006 05:30 PM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration