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 data to the first line of an existing file

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-02-09, 03:53
swatisurana2 swatisurana2 is offline
Registered User
 
Join Date: Mar 2009
Posts: 1
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
Reply With Quote
  #2 (permalink)  
Old 03-03-09, 01:43
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,258
Code:
f=abc.h.data
mv $f $f.old
sed '1 s/$/'$f'/' $f.old >$f
Reply With Quote
  #3 (permalink)  
Old 03-03-09, 06:42
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,578
You can additionally use sed's -i or --in-place option.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 03-03-09, 07:54
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,258
Not available on my system

# sed -i
sed: illegal option -- i
Reply With Quote
  #5 (permalink)  
Old 03-04-09, 04:08
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,578
Oh, I assumed that pretty much everyone is using the GNU tools these days...
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 03-04-09, 04:34
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Quote:
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.
Reply With Quote
  #7 (permalink)  
Old 03-09-09, 11:38
sundeepsharma sundeepsharma is offline
Registered User
 
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
Reply With Quote
Reply

Thread Tools
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