Adding header to an existing file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding header to an existing file
# 1  
Old 07-23-2008
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
# 2  
Old 07-23-2008
Code:
sed -e '1i\HeaderGoesHere' originalFile > newFile

# 3  
Old 07-23-2008
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
# 4  
Old 07-23-2008
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

# 5  
Old 07-24-2008
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 05:56 AM..
# 6  
Old 07-24-2008
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python DictWriter header - not writing in first line of existing file

Hello I am facing a very unique problem and not able to understand why. I have written a function which will check header of the file. If header is present good else it will write the header on top def writeHeaderOutputCSV(fileName): # See if the file exist already try: ... (0 Replies)
Discussion started by: radioactive9
0 Replies

2. Shell Programming and Scripting

Need help in adding header of a file

Hi All , I have a pipe dilimited file .Sample file is below.I need to add header in that file through unix. 000001| 1|AQWWW|234,456.00 | | 123456| |41|abC| 0|xyZ| 000002| 2|11 4|1,234,456.99| | 0| |23| |99|! | 000003| 3|!!@#$|0,000,001.10| | ... (4 Replies)
Discussion started by: STCET22
4 Replies

3. Shell Programming and Scripting

adding header in a file

Hi team, In my script i am storing some value in a variable "header". I want to add the header value has header in a file. Please help me on this Thanks in advance, Baski (4 Replies)
Discussion started by: baskivs
4 Replies

4. UNIX for Dummies Questions & Answers

Adding header and trailer into a file

Hi, I want to add the below Header to all the files in sequence File1,File2,File3...etc "ABC,<number of chracter in the file>" e,g - If File1 is as below pqrstuvdt abcdefgh then I want to add the above header into it ,So that File1 becomes as below ABC,17 pqrstuvdt abcdefgh ... (9 Replies)
Discussion started by: spari2
9 Replies

5. Shell Programming and Scripting

Adding Header and Trailer records to a appended file

How can we a shell script and pass date parameters .I have 3 files comming from Datastage with |" delimited I need append 3 files as above: File1: P0000|"47416954|"AU|"000|"INS|"0000|"|"20060601|"99991231|"|"|"|"|"01 File 2:... (2 Replies)
Discussion started by: e1994264
2 Replies

6. Shell Programming and Scripting

Adding existing set of records in the same file

I have a file with 50,000 records in it, i have a requirement to use the same 50,000 records and add them 4 times to the same file to make a total of 200,000 records. I was wondering how to do this using ksh. Any help is greatly appreciated. (2 Replies)
Discussion started by: vpv0002
2 Replies

7. Shell Programming and Scripting

Adding a new column in a file with other existing columns

Hi All , Kindly help me with this soln awk '{printf "%s %7s \n", $1,$c}' infile where value of variable c I am externally giving input But executing the above command shows all the columns of infile where as I want only 1st column of infile and 2nd column should print value c (8 Replies)
Discussion started by: Pratik4891
8 Replies

8. UNIX for Dummies Questions & Answers

Adding a header to a log file

Hello, I’m trying to add a row that will server as the header for a space separated file. 1-I have a number of files save in a directory 2- grep text path/*.log > newfile newfile looks like this Field1 Field2 Field3 Field4 Field1 Field2 Field3 Field4 Field1 Field2 Field3 Field4 Field1... (2 Replies)
Discussion started by: rene reivera jr
2 Replies

9. Shell Programming and Scripting

Adding file to an existing tar

Hi Friends, I want to know the command to add a new file in a existing tar file. For Ex: I have a tar file file1.tar with the contents one.txt two.txt three.txt Now I need to add file four.txt to this existing tar file, how can I do it? Thanks in advance (4 Replies)
Discussion started by: mr_manii
4 Replies

10. Shell Programming and Scripting

Need Help for Adding Three new columns in existing file from fatching data from file

not required this time (36 Replies)
Discussion started by: Sandeep_Malik
36 Replies
Login or Register to Ask a Question