LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Append text on top (https://www.linuxquestions.org/questions/linux-newbie-8/append-text-on-top-681072/)

ShaqDiesel 11-04-2008 11:24 AM

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?

arizonagroovejet 11-04-2008 11:48 AM

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
cat file_to_append_to_top_of >> /tmp/blah
cat /tmp/blah > file_to_append_to_top_of
rm /tmp/blah

Use a better filename than /tmp/blah. (look at mktemp command) Or you could use tac to reverse the lines in file_to_append_to_top_of, append to the end, then use tac to reverse again but that's no better than the above.

john test 11-04-2008 11:53 AM

can you append to the existing file and then do a sort to get the lines in proper order?

arizonagroovejet 11-04-2008 11:56 AM

Quote:

Originally Posted by john test (Post 3331484)
can you append to the existing file and then do a sort to get the lines in proper order?

In theory I guess. But in practice I don't see how that wouldn't be far more complicated.

sydney-troz 11-04-2008 02:22 PM

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
cat /tmp/temp-file file_to_append_to_top_of

This assumes that you know you'll want your data on top of the file, not sorted in a different way.

john test 11-04-2008 02:56 PM

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
2. tac storagefile
3. discovery process >> storagefile
4. tac storagefile
5. pause and wait for new input or Display request
6. goto 2 for new input
7. Display Contents of storagefile
8. goto 5


Telemachos 11-04-2008 06:53 PM

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
first
second
third

telemachus ~ $ cat extra
prefirst
other

Assume that you are searching for "prefirst" in the file extra with grep and you want to put that on the top of original (and save a backup to original.bak - just in case something goes very wrong).
Code:

telemachus ~ $ perl -i.bak -ple 'open($grep, "grep pre extra|"); $prepend = <$grep>; s/$_/$prepend$_/ if $. == 1' original
Result:
Code:

telemachus ~ $ cat original
prefirst
first
second
third

It works, but in more complex situations, I can't recommend it.


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