Adding header to sub files after splitting the main file using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding header to sub files after splitting the main file using AWK
# 1  
Old 03-27-2012
Adding header to sub files after splitting the main file using AWK

Hi Folks,

I have a file like:

mainfile.txt:
-------------
Code:
file1	abc	def	xyz
file1	aaa	pqr	xyz
file2	lmn	ghi	xyz
file2	bbb	tuv	xyz

I need output having two files file1 and file2.

file1:
------
Code:
Name	 State	Country
abc	 def	        xyz
aaa	 pqr 	        xyz

file2:
------
Code:
Name	 State	Country
lmn	 ghi	        xyz    
bbb	 tuv	        xyz

I was able to achieve the desired output using command:
Code:
awk -F '\t' '{FILENAME=$1}{print $2 "\t" $3 "\t" $4 > FILENAME}' mainfile.txt

It is creating the two required files. But the only problem is inserting the header. I tried using some thing as:
Code:
awk -F '\t' '{FILENAME=$1}{if(NR==1) print "Name \t State \t Country"} {print $2 "\t" $3 "\t" $4 > FILENAME}' mainfile.txt

The issue here is that the header is getting inserted only in the first file. Can somebody please help me out? I'll be very grateful.

Thanks,
Tanmay


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 03-27-2012 at 03:35 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 03-27-2012
Hi


Code:
awk -F '\t' '{if(FILENAME!=$1){FILENAME=$1;print "Name \t State \t Country" > FILENAME}} {print $2 "\t" $3 "\t" $4 > FILENAME}'  mainfile.txt

Guru.
# 3  
Old 03-27-2012
Also if there are many different files in column 1 of the real input file, you need to close them afterwards otherwise awk will complain that there are too many files open...
Code:
awk -F'\t' 'f!=$1{f=$1;print h>f}{print $2,$3,$4>>f; close(f)}' h="Name\tState\tCountry" OFS='\t' infile

# 4  
Old 03-27-2012
MySQL

Thank you Franklin52 for editing my post, I'll keep those things in mind going ahead.

Thanks a lot Guru & Scrutinizer, your replies definitely helped me and they worked. Thank you very much guys. Smilie

BR,
Tanmay
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 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

Splitting Files with awk into other directory

I am trying to split into different files using awk: cat files | gawk '$1 ~ /---/ || $1 ~ /^deleting$/ || $1 ~ /^sorting$/ || $1 ~ /==/ {print}'| gawk '$1 ~ /---/ || $1 ~ /^deleting$/ || $1 ~ /^sorting$/ || $1 ~ /==/ {print}' |gawk '//{x="F"++i;}{print > x;}' What I am trying to do is make F*... (3 Replies)
Discussion started by: newbie2010
3 Replies

4. Shell Programming and Scripting

Exclude the header row while splitting the file

Hi All, i have script like ... "TYPE_ID" "ID" "LIST_ID" "18" "52010" "1059" "18" "52010" "1059" "18" "52010" "1059" "18" "52010" "1059" i am using the below code it's not taking the header row. awk -F"\t" -v file=test1.txt -v file1=test2.txt ' { if(... (7 Replies)
Discussion started by: bmk
7 Replies

5. 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

6. 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

7. 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

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. UNIX for Dummies Questions & Answers

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... (5 Replies)
Discussion started by: shash
5 Replies

10. Shell Programming and Scripting

Splitting input files into multiple files through AWK command

Hi, I needs to split *.txt files from single directory depends on the some mutltiple input values. i have wrote the code like below for file in *.txt do grep -i -h "value1|value2" $file > $file; done. My requirment is more input values needs to be given in grep; let us say 50... (3 Replies)
Discussion started by: arund_01
3 Replies
Login or Register to Ask a Question