Operating System - HP-UX
1754964 Members
3246 Online
108828 Solutions
New Discussion юеВ

Merging lines into one from one file using awk or gawk

 
SOLVED
Go to solution
Chris Frangandonis
Regular Advisor

Merging lines into one from one file using awk or gawk

Hi All,


I am trying to merge lines using the first coloum as a refrence
File

1,113.979762606241,117.15991262116 <----
2,113.920187943931,117.121081977179
3,113.831185137368,117.062414176877
4,113.747543486782,117.007098544748
5,113.662300379663,116.941934474729
6,113.574862112705,116.87894936554
7,113.481621684309,116.775009017227
8,113.412866953385,116.737787070401
9,113.31232777743,116.669292852059
10,113.242558581134,116.647779295117
1,109.536550756229,112.474786932381 <---
2,109.509733027403,112.442740944945
3,109.452412898058,112.381017714262
4,109.409086413156,112.324590577287
5,109.342919929612,112.254229739005
6,109.284422446846,112.170057564478
7,109.231926893523,112.100344269982
8,109.198931583295,112.070743094249
9,109.146710930887,111.993124769744
10,109.090820864301,111.927757056819

output required

1,113.979762606241,117.15991262116,109.536550756229,112.474786932381
2,113.920187943931,117.121081977179,109.509733027403,112.442740944945
3,113.831185137368,117.062414176877,109.452412898058,112.381017714262
4 etc .....


My Script so far is

gawk 'BEGIN{FS=","}
{if($1~/[0-9]/)
{
arr[$1]=$1
arrF[$2,$3]=$0
}
}
END{
for (i in arr)

printf("%s\n", i,arr[i];
}' $1

Many thanks
Chris
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Merging lines into one from one file using awk or gawk

Hi Chris:

# ./myjoins
#!/usr/bin/perl
use strict;
use warnings;
my %joined;
my $key;
while (<>) {
chomp;
($key) = split /,/;
push( @{ $joined{$key} }, $_ );
}
foreach $key ( sort { $a <=> $b } keys %joined ) {
print "@{$joined{$key}}\n";
}

...run as:

# ./myjoins file

Regards!

...JRF...
Chris Frangandonis
Regular Advisor

Re: Merging lines into one from one file using awk or gawk

Hi James,

Nice one thanks.

How about in awk/gawk?

Thanks
Chris
James R. Ferguson
Acclaimed Contributor
Solution

Re: Merging lines into one from one file using awk or gawk

Hi (again) Chris:

This is a more correct rendering of your desired output, fisrt in Perl and then in 'awk':

# cat ./myjoins.pl
#!/usr/bin/perl
use strict;
use warnings;
my ( %joined, $key, @rest );
while (<>) {
chomp;
( $key, @rest ) = split /,/;
if ( exists $joined{$key} ) {
push( @{ $joined{$key} }, @rest );
}
else {
push( @{ $joined{$key} }, $_ );
}
}
foreach $key ( sort { $a <=> $b } keys %joined ) {
printf "%s\n", join ",", @{ $joined{$key} };
}
1;

...and in 'awk' :

# cat ./myjoins.awk
#!/usr/bin/awk -f
BEGIN{FS=","}
{
key=$1
if (key in arr) {
arr[key]=arr[key]$2","$3
}
else
arr[key]=$0","
}
{ next }
END {
print "the_end"
for (i in arr)
printf("%s\n",arr[i]);
} $1


...run as :

# ./myjoins.pl file

# ./myjoins.awk file

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Merging lines into one from one file using awk or gawk

Hi (again) Chris:

...and if you want the output of the 'awk' script to match that of the Perl script (which does an internal sort), simply do:

# ./myjoins.awk file | sort -kn1,1

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Merging lines into one from one file using awk or gawk

>JRF: ./myjoins.awk file | sort -kn1,1

The documentation says you need:
... | sort -k1n,1

In your END, you don't really need printf:
for (i in arr)
print arr[i]
Chris Frangandonis
Regular Advisor

Re: Merging lines into one from one file using awk or gawk

Hi James, Dennis

Its perfect.

Thanks Again
Chris