Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Posts
    1

    Unanswered: How to append data to the first line of an existing file

    I have a file named "abc.h.data"

    Its contents are

    HDR20090113
    abcde
    adcerty
    gtyhsrd
    ...
    TRL20090113

    What I want is to add the filename (i.e. abc.h.data) to the end of the first line so that content looks like the following


    HDR20090113abc.h.data
    abcde
    adcerty
    gtyhsrd
    ...
    TRL20090113


    also, I am reading the filename from command line.


    Kindly help

    Thanks

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Code:
    f=abc.h.data
    mv $f $f.old
    sed '1 s/$/'$f'/' $f.old >$f

  3. #3
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    You can additionally use sed's -i or --in-place option.
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  4. #4
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Not available on my system

    # sed -i
    sed: illegal option -- i

  5. #5
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    Oh, I assumed that pretty much everyone is using the GNU tools these days...
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  6. #6
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    What I want is to add the filename (i.e. abc.h.data) to the end of the first line so that content looks like the following
    I couldn't understand why he would want to add the filename to the file in the first place. Deciding to append it to the end of the first row with no separator just seems like creating work for yourself (well others in this case ) unless it's coursework.

  7. #7
    Join Date
    Mar 2009
    Posts
    4
    Suppose your file name is abc.dat then you cn use
    awk '{ if(NR==1) {print $0 FILENAME} if(NR>1) print $0 }' abc.dat

Posting Permissions

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