LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-06-2010, 05:49 PM   #16
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251

What about:
Code:
sed '2i\2' infile
I guess this one isn't going to work either.
 
Old 11-06-2010, 05:51 PM   #17
SentralOrigin
Member
 
Registered: Jul 2005
Distribution: Gentoo, Ubuntu
Posts: 318

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by sycamorex View Post
What about:
Code:
sed '2i\2' infile
I guess this one isn't going to work either.
Ya, no dice

Code:
sed: 1: "2i\2": extra characters after \ at the end of i command
 
Old 11-06-2010, 05:54 PM   #18
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
OK, so the solution seems to be, match something that guarantees you match the TITLE line of the file, and insert another line like:
Code:
root@reactor: cat file.txt
Here's the title line..
Here's some junk..
more stuff.
2
3
4
5
blah blah

root@reactor: sed -i 's/\(^.*title.*$\)/\1\nHERE IS MY NEW LINE/' file.txt
root@reactor: cat file.txt
Here's the title line..
HERE IS MY NEW LINE
Here's some junk..
more stuff.
2
3
4
5
blah blah

root@reactor:
Note that I used the -i option to sed, to edit in-place. If you want the output to screen or a new file, remove the -i.

UPDATE: Here's an awk way, but awk doesn't edit in-place, so you need to direct output to a new file, then copy/replace the new file over the old file:
Code:
awk '{if(NR==1){print $0;print "HERE IS MY NEW LINE"} else {print}}' file.txt
This method does not depend on matching anything in the title line; it just prints your new line after print line #1 of the file.

Last edited by GrapefruiTgirl; 11-06-2010 at 06:03 PM.
 
1 members found this post helpful.
Old 11-06-2010, 06:04 PM   #19
SentralOrigin
Member
 
Registered: Jul 2005
Distribution: Gentoo, Ubuntu
Posts: 318

Original Poster
Rep: Reputation: 30
Update: My mistake, I thought it worked but it didn't:

Code:
$ sudo sed 's/\(^.*1.*$\)/\1\\n2/' file.txt
1n2
3
4
Looks like the \n newline doesn't work. It prints 'n' instead
If I remove the newline, it just prints the 2 right next to the 1.

Last edited by SentralOrigin; 11-06-2010 at 06:12 PM.
 
Old 11-06-2010, 06:08 PM   #20
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
I don't really understand how (why) the command in post #19 works.. Great if it does.. But maybe check that it works on the real data too, not just this test case.
Edit - the command does not work.

Edit: Point me to this thread one day when I am wondering if I should try out *BSD :-D

Last edited by GrapefruiTgirl; 11-06-2010 at 06:21 PM.
 
Old 11-06-2010, 06:09 PM   #21
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
disregard
 
Old 11-06-2010, 06:15 PM   #22
Squall90
Member
 
Registered: Oct 2009
Distribution: Currently several distros :S
Posts: 148

Rep: Reputation: 29
It seems, OpenBSD uses it's own sed, not GNU sed -> http://www.openbsd.org/cgi-bin/cvswe...e=text%2Fplain

However, here's the man page if there are another problems with the command: http://www.openbsd.org/cgi-bin/man.c...=sed&sektion=1
 
Old 11-06-2010, 06:17 PM   #23
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
Here's something else interesting, which probably will not work:
Code:
sed 's/\(^.*title.*$\)/echo \1;echo "HERE IS MY NEW LINE"/e' file.txt
This uses the /e switch to sed, which executes commands on the RHS of the command. Probably you do not have /e on your sed, but who knows..
And you mentioned you don't have the -i option, so no matter what you do (with sed anyhow), it looks like you're needing to direct output to new file, and then copy it over the old one.

EDIT: And, just read the manpage provided by Squall90 - forget the /e option by the looks of things.

Last edited by GrapefruiTgirl; 11-06-2010 at 06:19 PM.
 
Old 11-06-2010, 07:19 PM   #24
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Ok, I powered on my Freebsd virtual machine and did some googling. The GNU version of sed is richer in features. On the BSD version of sed you can achieve it as follows:

Code:
sed -e "2i\\[Press Enter]
2" infile
 
  


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 append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
[Java] How to append something to each line in a text file ? Kunsheng Programming 3 04-15-2009 09:08 PM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
append a line to text file in script roadrash Linux - Newbie 4 10-01-2008 09:03 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM

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

All times are GMT -5. The time now is 12:41 AM.

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