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 append a line to the end of the file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-06-04, 09:54
sachin_mt sachin_mt is offline
Registered User
 
Join Date: Jan 2003
Posts: 106
how to append a line to the end of the file

Hi,

pl. provide the command to append a text say "i am fine" to the end of a file say abc.txt using shell script.

-sachi
__________________
Sachi
Reply With Quote
  #2 (permalink)  
Old 04-06-04, 10:13
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Re: how to append a line to the end of the file

Quote:
Originally posted by sachin_mt
Hi,

pl. provide the command to append a text say "i am fine" to the end of a file say abc.txt using shell script.

-sachi
echo "i am fine" >> abc.txt
Reply With Quote
  #3 (permalink)  
Old 04-06-04, 10:15
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
You can also do something like this (if you are vicious) :
Code:
# With sed
sed '$a\
i am fine' abc.txt > tmp.txt ; mv tmp.txt abc.txt

# With awk
awk '1 END {print "i am fine"}' abc.txt > tmp.txt ; mv tmp.txt abc.txt

# Other way
{ cat abc.txt; echo 'i am fine'; } > tmp.txt ; mv tmp.txt abc.txt
__________________
Jean-Pierre.

Last edited by aigles; 04-06-04 at 10:27.
Reply With Quote
  #4 (permalink)  
Old 04-06-04, 10:35
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Code:
{ cat abc.txt; echo 'i am fine'; } > tmp.txt ; mv tmp.txt abc.txt
While I'm not sure if this is necessary for the task the OP has outlined, the following method to avoid the use of a temp file might be of interest...
Code:
( rm abc.txt; { cat; echo "i am fine"; } > abc.txt ) < abc.txt
Basically, the use of the subshell allows the filename 'abc.txt' to be removed from the file system while continuing to hold the the file open. Because the filename no longer exists, the process within the subshell can create that filename without destroying the input.

Not to everyone's taste but I think it's a pretty cool trick!
Reply With Quote
  #5 (permalink)  
Old 04-06-04, 10:49
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Cool !
But i think i will never use this technique...
__________________
Jean-Pierre.
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