The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading the header of a tar file(posix header) Tanvirk Linux 2 01-31-2008 05:50 AM
existing file to a fixed length file cmanand Shell Programming and Scripting 3 01-25-2008 02:50 PM
adding existing disks to a 3510 array malcqv SUN Solaris 2 12-21-2007 01:40 AM
SMF - Adding instances to existing manifest craigp84 SUN Solaris 2 10-01-2007 03:55 AM
i want to delete a file based on existing file in a directory srivsn Shell Programming and Scripting 3 04-11-2006 01:38 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-23-2008
Registered User
 

Join Date: Jan 2007
Posts: 17
Adding header to an existing file

Dear All,

I need to add a header of one line to an already existing file.
I know that it can be achieved by the following:

echo "Header" > newfile
cat file1 >> newfile

But my problem is that file is huge and there is no space for creating a new file every time. Is there a way that I can add header without creating a new file?

Thanks
Shash
Reply With Quote
Forum Sponsor
  #2  
Old 07-23-2008
sysgate's Avatar
Unix based
 

Join Date: Nov 2006
Location: /root
Posts: 1,200
Code:
sed -e '1i\HeaderGoesHere' originalFile > newFile
Reply With Quote
  #3  
Old 07-23-2008
Registered User
 

Join Date: Jan 2007
Posts: 17
Thanks for the reply sysgate. But, I don't want to create a new file.
Is there a way I can add the header in the existing file itself.

Thanks
Shash
Reply With Quote
  #4  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Location: Texas
Posts: 60
If this is something you will need to do repeatedly, I suppose you could try something with an ex script. If you had something like this in a file named addhdr:
Code:
 
1i
this is what my header will be
.
x
And you would run it like this:
Code:
 
ex - myreallybigfile < addhdr
Reply With Quote
  #5  
Old 07-24-2008
Registered User
 

Join Date: Jan 2007
Posts: 17
Thanks Vi-Curious!. It works for a small file. But I'm encountering an error stating Tmp file too large
while carrying out for a much bigger file.

Thanks
Shash

Last edited by shash; 07-24-2008 at 01:56 AM.
Reply With Quote
  #6  
Old 07-24-2008
Registered User
 

Join Date: Jul 2008
Location: Texas
Posts: 60
Ok, so your file is too large. We need to use a "large file" safe editor. Switch from ex to ed and modify the end of the command file.

File addhdr_ed
Code:
 
1i
this is the 1st line of the header
this is the 2nd line of the header (if needed)
.
w
q
Execute as before, but using ed:
Code:
 
ed - myreallylargefile < addhdr_ed
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 08:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0