Home > Archive > AWK > April 2007 > Merge files using awk??
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Merge files using awk??
|
|
| sjmulay@gmail.com 2007-04-09, 6:56 pm |
| 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 Papanagnou 2007-04-09, 6:56 pm |
| sjmulay@gmail.com wrote:
> 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.
How about posting a few lines of concrete sample data and one or two
lines of expected result?
Janis
> any ideas?
> Thanks,
> Subodh
>
| |
|
| On Apr 9, 11:45 am, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> sjmu...@gmail.com wrote:
>
> How about posting a few lines of concrete sample data and one or two
> lines of expected result?
>
> Janis
>
>
>
>
> - Show quoted text -
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)
---------------------------------------------------
1 111 445 666 777
1 abcdefghij
1 111 234 999
1 6676658778
mergedfile2 (merging file2, file4, file5 based on 222)
----------------
1 222 445 666 777
1 abcdefghij
1 222 234 999
1 6676658778
1 222 11111111
1 6676658778
| |
| Ed Morton 2007-04-09, 6:56 pm |
| sjm wrote:
> On Apr 9, 11:45 am, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
>
> 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)
> ---------------------------------------------------
> 1 111 445 666 777
> 1 abcdefghij
> 1 111 234 999
> 1 6676658778
>
> mergedfile2 (merging file2, file4, file5 based on 222)
> ----------------
> 1 222 445 666 777
> 1 abcdefghij
> 1 222 234 999
> 1 6676658778
> 1 222 11111111
> 1 6676658778
>
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.
| |
| Vassilis 2007-04-09, 6:56 pm |
|
=CF/=C7 sjm =DD=E3=F1=E1=F8=E5:
> On Apr 9, 11:45 am, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
> 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)
> ---------------------------------------------------
> 1 111 445 666 777
> 1 abcdefghij
> 1 111 234 999
> 1 6676658778
>
> mergedfile2 (merging file2, file4, file5 based on 222)
> ----------------
> 1 222 445 666 777
> 1 abcdefghij
> 1 222 234 999
> 1 6676658778
> 1 222 11111111
> 1 6676658778
Use this command to merge your files according to the 2nd field found
on the first line of each input file:
awk 'FNR =3D=3D 1 { flag =3D $2 } flag =3D=3D 111 { print > "mergedfile1" }=
flag
=3D=3D 222 { print > "mergedfile2" }' file[1-5]
If you want some other field, change $2, field 2, to your likenings.
Vassilis
| |
| Janis Papanagnou 2007-04-09, 6:56 pm |
| sjm wrote:
> On Apr 9, 11:45 am, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
>
> 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)
> ---------------------------------------------------
> 1 111 445 666 777
> 1 abcdefghij
> 1 111 234 999
> 1 6676658778
>
> mergedfile2 (merging file2, file4, file5 based on 222)
> ----------------
> 1 222 445 666 777
> 1 abcdefghij
> 1 222 234 999
> 1 6676658778
> 1 222 11111111
> 1 6676658778
>
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
| |
|
| On Apr 9, 12:16 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> sjm wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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- Hide quoted text -
>
> - Show quoted text -
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 Papanagnou 2007-04-10, 6:56 pm |
| sjm wrote:
> On Apr 9, 12:16 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
> 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.
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
> Thanks again
>
| |
|
| On Apr 10, 9:44 am, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> sjm wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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
>
>
>
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
Thanks Janis
|
|
|
|
|