LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-04-2008, 12:24 PM   #1
ShaqDiesel
Member
 
Registered: Jul 2005
Posts: 144

Rep: Reputation: 15
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?
 
Old 11-04-2008, 12:48 PM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
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.
 
Old 11-04-2008, 12:53 PM   #3
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
can you append to the existing file and then do a sort to get the lines in proper order?
 
Old 11-04-2008, 12:56 PM   #4
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Quote:
Originally Posted by john test View Post
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.
 
Old 11-04-2008, 03:22 PM   #5
sydney-troz
Member
 
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Rep: Reputation: 15
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.
 
Old 11-04-2008, 03:56 PM   #6
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
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
 
Old 11-04-2008, 07:53 PM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
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.

Last edited by Telemachos; 11-04-2008 at 07:55 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
append the text at the end wanghao Linux - Server 1 12-14-2007 12:17 PM
[C++] append to each row of a text files sylvaticus Programming 2 10-18-2007 08:22 AM
How can I append text to a string in a file HGeneAnthony Linux - Newbie 4 03-01-2007 01:16 PM
sed script to append variable text gmartin Linux - General 4 12-27-2006 05:44 PM
quickly append text to file blackzone Linux - General 3 09-07-2006 06:48 AM


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

Main Menu
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration