LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > linux-related notes
User Name
Password

Notices


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Append something to the top of a file, in bash, without a temporary file

Posted 09-21-2013 at 03:04 PM by the dsc
Updated 09-21-2013 at 03:06 PM by the dsc

Using echo "something" >> extantfile.wtv may be handy way to add something to a file, but it will only add to the end.

The closest thing one would more likely imagine to add something at the top would be something like combining echo/whatever and a cat into a temporary file, and then moving it into the old file. Or maybe something crazy with tac and rev.

But it's possible to skip the temporary file:

echo -e "$(grep -A 1 patternX source-of-bit-to-append)\n$(cat file-to-have-the-bit-added-to-the-top)" > file-to-have-the-bit-added-to-the-top

That will add the output of grep (the line with "patternX" and one line after that, taken from some other file) to the top of the file at the end.
Posted in Uncategorized
Views 1021 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 09:53 PM.

Main Menu
Advertisement
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