How to append text to the second line of a file
Say I have a text file like:
Code:
1 Edit: Okay it's not bash, it's ksh because this computer is OpenBSD |
This should work:
Code:
sed '1a\2' infile |
Hey,
I get the error: Code:
$ sed '1a\2' infile |
Is it the EXACT output of your command?
|
What do you mean? What you see is the exact same thing I get
Code:
$ sed '1a\2' infile |
That's weird. It works for me:
Code:
sycamorex@mainframe:~/temp$ less file.txt |
sycamorex's idea also works fine for me. Maybe a version difference? I have:
Code:
root@reactor: sed --version Code:
root@reactor: cat file.txt |
Can you try:
Code:
sed '1a2' infile |
Ah, well...
Is there some sort of alternative to sed? |
Quote:
Code:
$ sed '1a2' file.txt |
Quote:
Code:
$ sed --version |
Yeah, what distro and sed version are you using?
1a2 works for me as well. Edit: I use the same version as GrapefruiTgirl does. |
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 |
Quote:
|
Quote:
|
What about:
Code:
sed '2i\2' infile |
Quote:
Code:
sed: 1: "2i\2": extra characters after \ at the end of i command |
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 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 |
Update: My mistake, I thought it worked but it didn't:
Code:
$ sudo sed 's/\(^.*1.*$\)/\1\\n2/' file.txt If I remove the newline, it just prints the 2 right next to the 1. |
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 |
disregard
|
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 |
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 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 |
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] |
All times are GMT -5. The time now is 07:05 AM. |