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.
Append something to the top of a file, in bash, without a temporary file
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.
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.
Total Comments 0