Inserting Header to another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting Header to another file
# 1  
Old 11-13-2014
Inserting Header to another file

Need your help in appending header(file1 contains header ) to my file2. I am using KSH AIX OS.
I know how to do with taking temporary files.
Code:
cat file1  >temp
 cat file2 >>temp
  mv temp  file2

Is there way to append directly to a file in ksh.

I don't find Sed -i option on my unix flavor.
# 2  
Old 11-13-2014
You can use awk, cat, ed, ex, sed (with or without -i), or lots of other methods. All of them are going to use a temp file to prepend data into an existing file. Some of them will make the temp file obvious; others will use the temp file under the covers.
# 3  
Old 11-13-2014
Can you please share me the method without using temp files. The problem I had is my data files are in GB's (20GB). My process takes more time to move 20GB of data into tmp file instead what I taught is adding a single line to 20GB data is much faster.
# 4  
Old 11-13-2014
Quote:
Originally Posted by gvkumar25
Can you please share me the method without using temp files. The problem I had is my data files are in GB's (20GB). My process takes more time to move 20GB of data into tmp file instead what I taught is adding a single line to 20GB data is much faster.
As I said before, there is no way to add data to the start of a file without copying the data at least once. If the file you're modifying has more than one link and you don't want to break the links or the temp file is on a different filesystem than the file you're modifying, you'll have to copy the data twice.

Even if you have more than 20B of memory you can allocate to a program to edit your file, you still have to read the entire 20GB into memory and write the entire 20+GB of data back into your file.

Of course you could create a new filesystem type that allows you to add data to either end of a file without moving existing data and create a new system call to write data into a file at negative offsets. (But, before you ask, I'll warn you that I'm not going to volunteer to design either of those for you for free in a forum like this!)
# 5  
Old 11-13-2014
Is that "Inserting Header to another file" an exercise on its own or is it done in preparation of another processing step? If the latter, can't you "add the header" in that step? e.g. awk '...' file1 file2 or cat file1 file2
# 6  
Old 11-13-2014
https://www.unix.com/answers-to-frequ...-original.html seems to be relevant here Smilie

Now that you know sed -i would create a "invisible" temp file too and your sed version does not support the -i option, you might want to try following Perl equivalent:

From perlfaq5 - perldoc.perl.org :
Code:
You can even add a line to the beginning of a file, since the current line prints at the end of the loop:

    perl -pi -e 'print "Put before first line\n" if $. == 1' inFile.txt

# 7  
Old 11-13-2014
How about our good ole ex utility...
Code:
ex -sc '0r file1 | x' file2

---------- Post updated at 12:01 PM ---------- Previous update was at 11:57 AM ----------

Quote:
Originally Posted by Don Cragun
You can use awk, cat, ed, ex, sed (with or without -i), or lots of other methods. All of them are going to use a temp file to prepend data into an existing file. Some of them will make the temp file obvious; others will use the temp file under the covers.
AFAIK ed and ex would use an internal buffer instead of a temporary file...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Shell Programming and Scripting

Inserting a header with special character

Hi, I am trying to insert header row with a special character delimiter with Unicode u0109 into a file with ‘echo’, header looks like below echo –e “header1\u0109header\u0109header3\u0109header4” It just inserting as it is in the quotes but not the special character, Please suggest if am... (2 Replies)
Discussion started by: oom
2 Replies

3. UNIX for Beginners Questions & Answers

Inserting Header at different position in a file

I would like to hear your directions on how to Insert theses tag </TITLE> and <TEXT> at a given position in 1000 of text files. My Files look like as samplefile1.txt <DOC> <DOCNO>3_September_2012</DOCNO> <TITLE> ... ... ... .... ... .. .. .. ... .. .. .... </TITLE> <TEXT> .... (1 Reply)
Discussion started by: imranrasheedamu
1 Replies

4. Shell Programming and Scripting

Inserting header after every nth line

Hi Experts, I have a file which contain hundreds of records/lines. I want to insert the below header in the file after every 60 lines. #Header FirstName LastName Address --------- ---------- --------- Let say I saved the... (6 Replies)
Discussion started by: brichigo
6 Replies

5. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

6. Shell Programming and Scripting

Comparing one file header with another file header

Hi Experts, In our project we have requirement where in we have to compare header of one file with header in the parameter file. There are 20 files which we ftp from one site. All this files have different header. We are comapring this file with our parameter file(which is having the header... (2 Replies)
Discussion started by: Amey Joshi
2 Replies

7. Ubuntu

Inserting a header with column number to a 1.6 GB file with special spacing

Hi; I've been searching posts to find a solution to what I'm trying to do, but I've have NOT found anything yet. I have a file (file1) with 300K columns and 1411 rows, the columns don't have a column no. header (No header at all) and I'm trying to fetch the information from specific columns.... (3 Replies)
Discussion started by: sogi
3 Replies

8. Shell Programming and Scripting

Inserting Header and footer

Hi All, I have several txt files i need to enter specific header and footer (both are separate) to all these files how can i do this? plz help.. Regards, Raghav (4 Replies)
Discussion started by: digitalrg
4 Replies

9. Shell Programming and Scripting

Inserting a String in a file header.

Dear all, I have a file created in the name sample.txt in UNIX with header and footer. How to insert a required string (for example "FILE1") in the header part after the file has been created. What kind of command can i use to do the same. Thanks in advance Hari (3 Replies)
Discussion started by: Hari123
3 Replies

10. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies
Login or Register to Ask a Question