LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-03-2010, 11:18 AM   #1
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Rep: Reputation: 0
Question problem with sed


Hello,

Could you help me with a sed command to add a text before line number in text file ?
I have text file with 500 lines, and i want to add 3 more lines with text after line 300, OR before line 302, isn't no problem.

Regards,
Eduard
 
Old 05-03-2010, 11:34 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I don't really understand your question, but as far as I can tell you want to insert some text after line 300?

Code:
sed -r '300 s:.*:&\nthis text will be inserted after line 300:'
Use \n to insert a newline, and \\ for a literal backslash.

Last edited by MTK358; 05-03-2010 at 11:36 AM.
 
Old 05-03-2010, 11:44 AM   #3
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
machine# sed -r '300 s:.*:&\nthis text will be inserted after line 300:' config-test > config-test1
sed: illegal option -- r
usage: sed script [-Ealn] [-i extension] [file ...]
sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]

You understand me correct. I want to insert some text after line 300.
And in other case i want to insert some text before line 100
I need help with theese two commands

Last edited by eradev; 05-03-2010 at 11:46 AM.
 
Old 05-03-2010, 11:45 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Code:
sed -e '300 a\
Add this line after line 300' inputfile > outputfile
Code:
sed -e '300 i\
Add this line before line 300' inputfile > outputfile

Last edited by crts; 05-03-2010 at 11:47 AM.
 
Old 05-03-2010, 11:50 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
The -r flag means "extended regular expressions". I find then better looking and easier to use. Maybe your version of sed just doesn't support it.
 
Old 05-03-2010, 11:57 AM   #6
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Code:
sed -e '300 a\
Add this line after line 300' inputfile > outputfile
Code:
sed -e '300 i\
Add this line before line 300' inputfile > outputfile
yep, i tried this before..

Code:
machine# sed -e '300 a\
? Add this line after line 300' config-test > config-test1
sed: 1: "300 a
Add this line aft ...": command a expects \ followed by text
machine# sed -e '300 a\ Add this line after line 300' config-test > config-test1
sed: 1: "300 a\ Add this line af ...": extra characters after \ at the end of a command
machine# sed -e '300 i\
? Add this line before line 300' config-test > config-test1
sed: 1: "300 i
Add this line bef ...": command i expects \ followed by text
machine#
machine# sed -e '300 i\ Add this line before line 300' config-test > config-test1
sed: 1: "300 i\ Add this line be ...": extra characters after \ at the end of i command
machine#
The OS is 7.2-RELEASE FreeBSD
 
Old 05-03-2010, 12:04 PM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by eradev View Post
yep, i tried this before..

Code:
machine# sed -e '300 a\
? Add this line after line 300' config-test > config-test1
sed: 1: "300 a
Add this line aft ...": command a expects \ followed by text
machine# sed -e '300 a\ Add this line after line 300' config-test > config-test1
sed: 1: "300 a\ Add this line af ...": extra characters after \ at the end of a command
machine# sed -e '300 i\
? Add this line before line 300' config-test > config-test1
sed: 1: "300 i
Add this line bef ...": command i expects \ followed by text
machine#
machine# sed -e '300 i\ Add this line before line 300' config-test > config-test1
sed: 1: "300 i\ Add this line be ...": extra characters after \ at the end of i command
machine#
The OS is 7.2-RELEASE FreeBSD
It is important that there is no space after \. Also, which version of sed are you using?

Can you try
Code:
machine# sed -e '300 a\Add this line after line 300' config-test > config-test1
 
Old 05-03-2010, 12:04 PM   #8
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
The -r flag means "extended regular expressions". I find then better looking and easier to use. Maybe your version of sed just doesn't support it.
Without -r flag it's work, but \n isn't insert new line..
 
Old 05-03-2010, 12:06 PM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by eradev View Post
Without -r flag it's work, but \n isn't insert new line..
Then it is probably your sed version because on my system MTK358's solution also worked.
 
Old 05-03-2010, 12:13 PM   #10
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
yes, probably, but \n isn't insert new line and I have to write 3 lines
 
Old 05-03-2010, 12:26 PM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by eradev View Post
yes, probably, but \n isn't insert new line and I have to write 3 lines
Ok, I do not like workarounds but since you have a 'weird' version of sed consider this workaround:
Code:
sed -n '1,300 p' input > tmp1
sed -n '301,$ p' input > tmp2
cat > insert_file << EOF
first line to be inserted after line 300
second line to be inserted after line 300
third line to be inserted after line 300
EOF
cat tmp1 insert_file tmp2 > finalout

Last edited by crts; 05-03-2010 at 12:28 PM.
 
Old 05-03-2010, 12:45 PM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
eradev;
You said you are using BSD. This reinforces the suggestion that you might have a different version of SED than what we are used to. What version are you using?
 
Old 05-03-2010, 01:29 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
\n does mean "newline" in sed (GNU sed, at least), just as it does in the C programming language.

If your sed is not GNU sed (which is standard for Linux), then we don't really know how to use it.
 
Old 05-03-2010, 01:43 PM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
http://www.grymoire.com/Unix/Sed.html (not written for gnu SED)
"\n" means newline here also....BUT:

When entering things in "real time" in a terminal, you use "\" to escape the newline that does not appear--eg

somecommand -flags arguments \
more arguements

I wonder if that is relevant to OP's issue?
 
Old 05-04-2010, 09:56 PM   #15
petrus4
LQ Newbie
 
Registered: May 2010
Posts: 9

Rep: Reputation: 1
Quote:
Originally Posted by eradev View Post
Hello,

Could you help me with a sed command to add a text before line number in text file ?
I have text file with 500 lines, and i want to add 3 more lines with text after line 300, OR before line 302, isn't no problem.

Regards,
Eduard
The sed answers are cool, and truthfully I didn't know you could use sed like that, but I would have used ed.

Code:
ed <file>
302i
This is the first line.
This is the second line.
This is the third line.
.
wq
It may seem strange, but I use ed quite a lot. I like it. I also feel that although it isn't really complicated, ed is sufficiently different to what you're used to, that if you want to learn to use it, you need to use it a lot in order to get familiar with it. Although I still use vi(m) for big files or longer edits, for me ed is very useful when I just want to do quick single line edits or search/replace for things; it's quicker than loading vi for that, and when you don't have to do much, the line addressing makes it a lot faster than vi's modal interface, as well.

Ed might also be better than sed in this case, because from what I've seen anywayz, sed isn't really designed to handle newlines. It can, but it's awkward.

Last edited by petrus4; 05-04-2010 at 10:03 PM.
 
  


Reply



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
sed problem kranthikumar.gatta Linux - Newbie 3 07-09-2008 02:16 AM
sed problem... jong357 Programming 11 05-09-2007 12:27 AM
Sed problem twintornado Programming 2 06-02-2005 08:25 AM
Sed problem Miro1 Programming 3 04-09-2005 09:02 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 PM.

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
Open Source Consulting | Domain Registration