LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-29-2008, 07:55 AM   #1
livetoday
Member
 
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.

Quote:
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, 08:45 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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:
http://www.grymoire.com/Unix/Sed.html
 
Old 03-29-2008, 09:12 AM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Jessie 8.4
Posts: 5,873

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
Seder's grab bag: http://sed.sourceforge.net/grabbag/tutorials/
 
Old 03-29-2008, 09:37 AM   #4
livetoday
Member
 
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 ......got 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, 08:45 AM   #5
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
Quote:
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:

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.

Regards, Feder
 
Old 03-31-2008, 12:28 AM   #6
livetoday
Member
 
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-31-2008 at 12:34 AM.
 
  


Reply


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 02:45 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM
using sed to insert lines with special characters disorderly Linux - Software 26 04-20-2006 06:30 PM
insert string with sed greg108 Programming 7 02-18-2005 02:11 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


All times are GMT -5. The time now is 11:27 AM.

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