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