LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-02-2009, 12:45 AM   #1
Atwin
Member
 
Registered: Apr 2004
Distribution: FreeBSD 8.0 & Fedora 12
Posts: 50

Rep: Reputation: 15
inserting text using sed


Hello guys

How can I insert text in front of line 18 and above in a text file using sed?

Thanks,

atwin
 
Old 02-02-2009, 12:54 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
I'm not clear what you want to do. "in front of line 18" could be done with the "i" command. The "and above" is what is confusing me. Do you want to insert the same text before each line up to the 18th line?

Also give more details on your particular problem so we know this isn't a homework problem. I don't want to help you cheat if that were the case.

Last edited by jschiwal; 02-02-2009 at 12:58 AM.
 
Old 02-02-2009, 06:50 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Really good SED tutorial here: http://www.grymoire.com/Unix/Sed.html

Look at addressing, and the "i" and "a" commands (insert and append)
 
Old 02-02-2009, 07:30 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Code:
18 {
  s/^/IN FRONT OF LINE 18/
  iABOVE LINE 18
}
Yves.
 
Old 02-02-2009, 05:00 PM   #5
Atwin
Member
 
Registered: Apr 2004
Distribution: FreeBSD 8.0 & Fedora 12
Posts: 50

Original Poster
Rep: Reputation: 15
By line 18 and above, I mean...lines 18,19,20,21...until end. I am new to unix/linux and I am trying to get familiar with the tools. I am 22 years old, homework is a thing of the past mate

Thanks for the help pixellany and theYinYeti.

Last edited by Atwin; 02-02-2009 at 05:03 PM.
 
Old 02-02-2009, 05:35 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,572
Blog Entries: 1

Rep: Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027
Code:
cat script.sed
Code:
18,$ {
  s/^/text_to_be_inserted/
}
Code:
sed -f script.sed file
 
Old 02-02-2009, 11:19 PM   #7
Atwin
Member
 
Registered: Apr 2004
Distribution: FreeBSD 8.0 & Fedora 12
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks Sycamorex. It worked fine.
 
  


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
RHEL 5 Kickstart copying files from DVD & inserting comments with sed ilo Linux - Enterprise 0 01-31-2008 12:01 PM
Inserting file contents into Sed Command ewingtux Programming 4 11-19-2007 06:59 PM
sed: delete text till <pattern2> depending on length of text oyarsamoh Programming 2 05-05-2007 01:40 AM
Inserting spaces using SED/AWK ? aravindts Programming 4 09-29-2006 05:28 AM
Inserting Text wildcat22 Linux - General 8 04-25-2004 07:51 AM


All times are GMT -5. The time now is 07:29 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration