Append text on top
I want to append the result of a grep on top of a file. For example:
I have a file with the following in it: filename2 filename3 grep gives me filename1 I want the file to contain: filename1 filename2 filename3 How can I append to the top of the file? How can I add newlines to the top? |
I've seen this question asked before and never seen an answer which doesn't involve a temporary file. E.g.
Code:
grep pattern filename > /tmp/blah |
can you append to the existing file and then do a sort to get the lines in proper order?
|
Quote:
|
In the specific case you gave initially, you know how the sorted data should look before you process it, so you could just do
Code:
grep pattern filename | cat - file_to_append_to_top_of >/tmp/temp-file |
For the case where random file names are discovered in random order and need to be stored in a file latest discovery on top:
Code:
1. discovery process > storagefile |
This little Perl one-liner would work for the situation you describe (if I understood it correctly): one file has a list of items; you grep a second file for one item, and if you find it, you want to add it to the top of the first file. Here's my starting files
Code:
telemachus ~ $ cat original Code:
telemachus ~ $ perl -i.bak -ple 'open($grep, "grep pre extra|"); $prepend = <$grep>; s/$_/$prepend$_/ if $. == 1' original Code:
telemachus ~ $ cat original |
All times are GMT -5. The time now is 07:53 PM. |