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.
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.
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
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#
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
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
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.