Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2003
    Posts
    106

    Unanswered: 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

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525

    Re: how to append a line to the end of the file

    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

  3. #3
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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
    Last edited by aigles; 04-06-04 at 11:27.
    Jean-Pierre.

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    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!

  5. #5
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Cool !
    But i think i will never use this technique...
    Jean-Pierre.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •