LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to append text to the second line of a file (https://www.linuxquestions.org/questions/programming-9/how-to-append-text-to-the-second-line-of-a-file-842783/)

SentralOrigin 11-06-2010 05:05 PM

How to append text to the second line of a file
 
Say I have a text file like:

Code:

1
3
4

How would I use ksh to put the number '2' into the second line of that file?

Edit: Okay it's not bash, it's ksh because this computer is OpenBSD

sycamorex 11-06-2010 05:15 PM

This should work:

Code:

sed '1a\2' infile

SentralOrigin 11-06-2010 05:24 PM

Hey,

I get the error:

Code:

$ sed '1a\2' infile
sed: 1: "1a\2": extra characters after \ at the end of a command


sycamorex 11-06-2010 05:33 PM

Is it the EXACT output of your command?

SentralOrigin 11-06-2010 05:35 PM

What do you mean? What you see is the exact same thing I get

Code:

$ sed '1a\2' infile
sed: 1: "1a\2": extra characters after \ at the end of a command
$


sycamorex 11-06-2010 05:37 PM

That's weird. It works for me:

Code:

sycamorex@mainframe:~/temp$ less file.txt
1
3
4
5
sycamorex@mainframe:~/temp$ sed '1a\2' file.txt
1
2
3
4
5


GrapefruiTgirl 11-06-2010 05:39 PM

sycamorex's idea also works fine for me. Maybe a version difference? I have:

Code:

root@reactor: sed --version
GNU sed version 4.2.1

UPDATE: And, it also appears to work without the backslash.. Perhaps try that?:
Code:

root@reactor: cat file.txt
1
3
4
5

root@reactor: sed '1a2' file.txt
1
2
3
4
5

root@reactor:


sycamorex 11-06-2010 05:39 PM

Can you try:

Code:

sed '1a2' infile

SentralOrigin 11-06-2010 05:40 PM

Ah, well...

Is there some sort of alternative to sed?

SentralOrigin 11-06-2010 05:41 PM

Quote:

Originally Posted by sycamorex (Post 4151390)
Can you try:

Code:

sed '1a2' infile

Code:

$ sed '1a2' file.txt
sed: 1: "1a2": command a expects \ followed by text
$


SentralOrigin 11-06-2010 05:41 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 4151389)
sycamorex's idea also works fine for me. Maybe a version difference? I have:

Code:

root@reactor: sed --version
GNU sed version 4.2.1

UPDATE: And, it also appears to work without the backslash.. Perhaps try that?:
Code:

root@reactor: cat file.txt
1
3
4
5

root@reactor: sed '1a2' file.txt
1
2
3
4
5

root@reactor:


Code:

$ sed --version
sed: unknown option -- -
usage: sed [-aEnru] command [file ...]
      sed [-aEnru] [-e command] [-f command_file] [file ...]
$ sed -version 
sed: unknown option -- v
usage: sed [-aEnru] command [file ...]
      sed [-aEnru] [-e command] [-f command_file] [file ...]
$ sed -v     
sed: unknown option -- v
usage: sed [-aEnru] command [file ...]
      sed [-aEnru] [-e command] [-f command_file] [file ...]
$

:/

sycamorex 11-06-2010 05:42 PM

Yeah, what distro and sed version are you using?

1a2 works for me as well.

Edit: I use the same version as GrapefruiTgirl does.

GrapefruiTgirl 11-06-2010 05:45 PM

I have seen this before, on weird versions of sed, like on Solaris and other non-Linux machines. They don't like to show what version they are or anything :/

It may help to know more about the larger project, in order to suggest a workaround that will work in all cases. Surely you don't have 1000 files that need a "2" added into them, so maybe there's a way to get the "big picture" accomplished, if we can understand what the whole project entails?

Plus - maybe sed can still be used, but instead of using the insert/append functionality, we'll just match a regex and add some stuff, like:
Code:

root@reactor: cat file.txt
1
3
4
5

root@reactor: sed 's/\(^3$\)/2\n\1/' file.txt
1
2
3
4
5

root@reactor:


SentralOrigin 11-06-2010 05:45 PM

Quote:

Originally Posted by sycamorex (Post 4151397)
Yeah, what distro and sed version are you using?

1a2 works for me as well.

Edit: I use the same version as GrapefruiTgirl does.

This is a BSD machine I think...sorry for being a newb, I'm not really familiar with this stuff

SentralOrigin 11-06-2010 05:48 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 4151400)
I have seen this before, on weird versions of sed, like on Solaris and other non-Linux machines. They don't like to show what version they are or anything :/

It may help to know more about the larger project, in order to suggest a workaround that will work in all cases. Surely you don't have 1000 files that need a "2" added into them, so maybe there's a way to get the "big picture" accomplished, if we can understand what the whole project entails?

Okay well this isn't really anything big, there is one particular file I would like to add a line to, but it has to go into the second line because it needs to come first. The first line is just a title. The file gets reset every few hours with random lines, and every time it changes I'd like to add my particular line to it, on the second line. I have to do it manually though

sycamorex 11-06-2010 05:49 PM

What about:
Code:

sed '2i\2' infile
I guess this one isn't going to work either.

SentralOrigin 11-06-2010 05:51 PM

Quote:

Originally Posted by sycamorex (Post 4151404)
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

GrapefruiTgirl 11-06-2010 05:54 PM

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.

SentralOrigin 11-06-2010 06:04 PM

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.

GrapefruiTgirl 11-06-2010 06:08 PM

I don't really understand how (why) the command in post #19 works.. :scratch: 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

sycamorex 11-06-2010 06:09 PM

disregard

Squall90 11-06-2010 06:15 PM

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

GrapefruiTgirl 11-06-2010 06:17 PM

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. :D

sycamorex 11-06-2010 07:19 PM

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



All times are GMT -5. The time now is 07:05 AM.