- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Merging lines into one from one file using awk or ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2009 10:18 AM
тАО03-30-2009 10:18 AM
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
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2009 10:48 AM
тАО03-30-2009 10:48 AM
Re: Merging lines into one from one file using awk or gawk
# ./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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2009 10:51 AM
тАО03-30-2009 10:51 AM
Re: Merging lines into one from one file using awk or gawk
Nice one thanks.
How about in awk/gawk?
Thanks
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2009 12:41 PM
тАО03-30-2009 12:41 PM
SolutionThis 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2009 12:51 PM
тАО03-30-2009 12:51 PM
Re: Merging lines into one from one file using awk or gawk
...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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2009 07:19 PM
тАО03-30-2009 07:19 PM
Re: Merging lines into one from one file using awk or gawk
The documentation says you need:
... | sort -k1n,1
In your END, you don't really need printf:
for (i in arr)
print arr[i]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2009 11:51 PM
тАО03-30-2009 11:51 PM
Re: Merging lines into one from one file using awk or gawk
Its perfect.
Thanks Again
Chris