If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > how to add line at bottom of file in linux

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-20-07, 08:54
dnyan dnyan is offline
Registered User
 
Join Date: Apr 2007
Posts: 2
Cool how to add line at bottom of file in linux

please tell me how to add line at bottom of file in linux by using a sed or awk ?
is a script for the same ?
Reply With Quote
  #2 (permalink)  
Old 04-21-07, 04:39
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Code:
echo "line" >> file
I'm sure awk can do that as well (for example in the END block), but why make it more complicated?
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 04-21-07, 06:42
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Hi,

I understood your question differently then Knut did and think something like this is what you're looking for.
Like Knut mentioned, here's an 'awk' example. The advantage is that first the complete file is read into memory and after that the memory content is written to the file on disk again (including the extra last line). With this you can overwrite the original file in one step because awk has finished reading the input filestream before the output filestream is created. (Offcourse this only works if there's enough memory available to store the complete file.)
Something that's going terribly wrong with 'sed', where you allways have to store the output in a helpfile first and overwrite the original with that.
Awk solution:
Code:
#!/bin/bash                             
                                        
awk '   {   line[NR] = $0   }           
    END {                               
            for(i = 1; i <= NR; i++)    
                print line[i] > FILENAME
            print "" > FILENAME         
        }' $1
Sed solution:
Code:
#!/bin/bash       
                  
sed '$s/$/\       
/' $1 > "${1}.tmp"
mv "${1}.tmp" $1
Regards

Last edited by Tyveleyn; 04-21-07 at 06:51.
Reply With Quote
  #4 (permalink)  
Old 04-21-07, 12:44
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Yeah, it really depends on what you want to do. Reading the entire file into main memory just to add a line at the end and leaving the rest of the file untouched would be overkill and a huge waste of resources, of course. Even if you need to modify the file, I would still recommend to not rely on main-memory. Only for really small (just a few MB) files, this is acceptable.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On