Merge files using awk??

Awk Programming

sjmulaygmailcom
Hi,
 I am trying to use awk to merge different files based on a certain
value at certain field position (this line/position will be fixed in
the every file)
e.g. lets say I have 5 files
2 files have the 111 at line 1, column 10
and the other 3 files have 222 at line1, column 10
I need to merge these 2 files based on the values at that line/column
position to create 2 files - one file that merged the 2 files having
111 and another file that merged the 3 files having 222.
any ideas?
Thanks,
Subodh                                            
Janis
How about posting a few lines of concrete sample data and one or two
lines of expected result?

Janis
                                            
sjm
Here it is ...
I wasnt posting the example in other post bcoz I could not see this
post for almost 45 minutes.
Thanks ..


e.g.

file1
1 111 445 666 777
1 abcdefghij

file2
1 222 445 666 777
1 abcdefghij

file3
1 111 234 999
1 6676658778

file4
1 222 234 999
1 6676658778

file5
1 222 11111111
1 6676658778

Result - Merged Files

mergedfile1 (merging file1 and file3 based on 111)
                                            
Vassilis
Ï/Ç sjm Ýãñáøå:


Use this command to merge your files according to the 2nd field found
on the first line of each input file:

awk 'FNR == 1 { flag = $2 } flag == 111 { print > "mergedfile1" } flag
== 222 { print > "mergedfile2" }' file[1-5]

If you want some other field, change $2, field 2, to your likenings.

  Vassilis
                                            
Ed
If you don't really care about the name of the merged file, just use the 
value of $2:

awk 'FNR==1{sfx=$2}{print > "mergedfile" sfx}' file1 file2 .... file5

If you do care, just map the $2s in an array...

	Ed.
                                            
Janis
The following awk program will produce the output in files which are
named according to the key, 111.out and 222.out ...

FNR==1 { filename = $2 }
{ print > filename ".out" }


Janis
                                            
sjm
Thank you Ed, Vassilis and Janis. I will try out these options. As of
now, I think I will need to have specific filename, but I will use
these suggestions before jumping too far ahead.
Thanks again
                                            
Janis
For specific filenames just add a mapping table (for example)...

BEGIN { map["111"] = "mergedfile1" ; map["222"] = "mergedfile2" }
FNR==1 { filename = map[$2] }
{ print > filename }


Janis
                                            
sjm
Thanks Janis