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


All times are GMT -5. The time now is 09:44 AM.