ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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....
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.
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.
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..
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."
}
}'
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."
}
}'
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.