LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-04-2010, 11:16 PM   #16
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170

Some ways of entering newlines in sed versions where \n doesn't work:-

"Frequently Asked Questions about sed, the stream editor.
4.1. How do I insert a newline into the RHS of a substitution?"
http://sed.sourceforge.net/sedfaq4.html#s4.1
 
Old 05-05-2010, 05:02 AM   #17
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by petrus4 View Post
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.
Hello,


How can i use this in script? I want to write this three lines with script with variables.
 
Old 05-05-2010, 05:06 AM   #18
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by petrus4 View Post
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.
Hello,


This example is for direct put in terminal. How can i use this in script? I want to write this three lines with script with variables like: ed $file $linei $text-line1 $text-line2....

sorry for my bad english :">
 
Old 05-05-2010, 05:58 AM   #19
petrus4
LQ Newbie
 
Registered: May 2010
Posts: 9

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


This example is for direct put in terminal. How can i use this in script? I want to write this three lines with script with variables like: ed $file $linei $text-line1 $text-line2....

sorry for my bad english :">
That's ok. From memory ed can be used in scripts, but I'm not entirely sure how. For scripts, sed is probably going to work better.
 
Old 05-05-2010, 06:55 AM   #20
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by eradev View Post
sorry for my bad english :">
Your English is a pleasure compared to many other members here.
 
Old 05-05-2010, 07:13 AM   #21
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
Your English is a pleasure compared to many other members here.
Thank you With this
Code:
sed -r '300 s:.*:&\nthis text will be inserted after line 300:'
is working, but i still cannot go to a new line. Everything is on same line. Do you know a way to pass a new line like pressing the button "enter"
 
Old 05-05-2010, 08:28 AM   #22
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
Thank you With this
Code:
sed -r '300 s:.*:&\nthis text will be inserted after line 300:'
is working, but i still cannot go to a new line. Everything is on same line. Do you know a way to pass a new line like pressing the button "enter"
Hi,

I just noticed the windows logo on the left of your posts. Are you using sed for windows files? If so, then you might have to replace the '\n' with '\r\n'. Just a thought.

You might also want to try
Code:
sed -e '300 s/.*/&\
this text will be inserted after line 300/' infile
This also worked for me. Some older versions of sed have problems with a '\n' character on the right side of the substitution command.
 
Old 05-05-2010, 08:51 AM   #23
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Hi,

I just noticed the windows logo on the left of your posts. Are you using sed for windows files? If so, then you might have to replace the '\n' with '\r\n'. Just a thought.

You might also want to try
Code:
sed -e '300 s/.*/&\
this text will be inserted after line 300/' infile
This also worked for me. Some older versions of sed have problems with a '\n' character on the right side of the substitution command.
I'm working on freebsd 7.2-release thro ssh.
Okay, do you know how i can with .sh script edit some line in file and insert 3 or 4 more lines with text on it?
say without sed..

Last edited by eradev; 05-05-2010 at 08:53 AM.
 
Old 05-05-2010, 09:11 AM   #24
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
AWK one-liner:

Code:
awk '{print; if (NR == 2) {print "this is\nsome text\ninserted after line 2."} }'
This was only tested on GNU awk, I can't guarantee it will work on FreeBSD's awk.

AWK script:

Code:
#!/bin/awk -f

{
	print
	if (NR == 2) # replace 2 with whatever line you want to insert text after
	{
		# an '\n' prints a newline. Note that a newline is automatically printed at the end
		print "this is\nsome text\ninserted after line 2."
	}
}'

Last edited by MTK358; 05-05-2010 at 09:16 AM.
 
Old 05-05-2010, 09:29 AM   #25
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
AWK one-liner:

Code:
awk '{print; if (NR == 2) {print "this is\nsome text\ninserted after line 2."} }'
This was only tested on GNU awk, I can't guarantee it will work on FreeBSD's awk.

AWK script:

Code:
#!/bin/awk -f

{
	print
	if (NR == 2) # replace 2 with whatever line you want to insert text after
	{
		# an '\n' prints a newline. Note that a newline is automatically printed at the end
		print "this is\nsome text\ninserted after line 2."
	}
}'
It's working! Thank you a lot man.
 
Old 05-05-2010, 09:31 AM   #26
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
I'm working on freebsd 7.2-release thro ssh.
Okay, do you know how i can with .sh script edit some line in file and insert 3 or 4 more lines with text on it?
say without sed..
Have you tried my suggestion in this post?
http://www.linuxquestions.org/questi...7/#post3955866
There sed is only used to split the file at insertion point. Afterwards three files are merged together, the middle one containing the lines you want to insert.
 
Old 05-05-2010, 10:40 AM   #27
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Have you tried my suggestion in this post?
http://www.linuxquestions.org/questi...7/#post3955866
There sed is only used to split the file at insertion point. Afterwards three files are merged together, the middle one containing the lines you want to insert.
It's working, but how can i get this in .sh script with variables?
I had a problem with awk integrated in .sh script with variables.

Last edited by eradev; 05-05-2010 at 10:58 AM.
 
Old 05-05-2010, 11:07 AM   #28
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by eradev View Post
I had a problem with awk integrated in .sh script with variables.
What problems exactly?

Where does the input come from (a variable, stdin, or a file) and where should the output go (a variable, stdout, or a file)?

If you can tell me that, I'll show you how to run AWK from your script appropriately.

Last edited by MTK358; 05-05-2010 at 11:10 AM.
 
Old 05-05-2010, 11:13 AM   #29
eradev
LQ Newbie
 
Registered: May 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
What problems exactly?

Where does the input come from (a variable, stdin, or a file) and where should the output go (a variable, stdout, or a file)?

If you can tell me that, I'll show you how to run AWK from your script appropriately.
something like that
Code:
/bin/cat $configfile | /usr/bin/awk '{print; if (NR == $startline) {print "$keyword\nmenu = $keyword1\ntitle = $keyword2 ($keyword3)\nhost = $keyword3\n"} }' > $tempconfigfile
without variables works great.
 
Old 05-05-2010, 11:13 AM   #30
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
It's working, but how can i get this in .sh script with variables?
I had a problem with awk integrated in .sh script with variables.
What are the variables? The text to be inserted? The files to be modified?
 
  


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
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 09:02 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