- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Script question - merging files based on first...
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
тАО07-26-2006 10:21 AM
тАО07-26-2006 10:21 AM
[ File-1 ]
Parameter,hostname1
NSTRBLKSCHED,-
NSTREVENT,50
NSTRPUSH,16
[ File-2 ]
Parameter,hostname2
NSTRBLKSCHED,-
NSTREVENT,100
NSTRPUSH,26
[ File-3 ]
Parameter,hostname3
NSTRBLKSCHED,-
NSTREVENT,150
NSTRPUSH,36
etc - etc - etc
I would like to read multiple input files, and if the first value in each line matches, output a single output line.
For example, from the above, I would get
Parameter,hostname1,hostname2,hostname3
NSTRBLKSCHD,-,-,-
NSTREVENT,50,100,150
NSTRPUSH,16,26,36
I would then use the csv file to create an xls comparing the kernal parms of multiple servers. Is there an easy way to merge files like this based on the 1st parameter ?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2006 11:30 AM
тАО07-26-2006 11:30 AM
Re: Script question - merging files based on first parameter
The following should serve your needs:
# cat cmpkparm
#!/usr/bin/perl
use strict;
use warnings;
my %lines;
my ( $parm, $value );
while (<>) {
chomp;
( $parm, $value ) = split( /,/, $_ );
if ( exists $lines{$parm} ) {
push( @{ $lines{$parm} }, $value );
}
else {
push( @{ $lines{$parm} }, $parm, $value );
}
}
foreach $parm ( sort keys %lines ) {
print "@{$lines{$parm}}";
print "\n";
}
1;
...Run as:
# ./cmpkparm file1 file2 file3 ...
That is, pass as many file names on the commandline as you need to process. The format of each file is as you posted.
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2006 01:22 PM
тАО07-26-2006 01:22 PM
Re: Script question - merging files based on first parameter
How do I get the commas between the values outputed ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2006 02:01 PM
тАО07-26-2006 02:01 PM
Re: Script question - merging files based on first parameter
awk -F, '{if(p[$1])p[$1]=p[$1]","$2;else p[$1]=$2}END{for(i in p) print i,p[i]}' files
cheers!
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2006 03:39 PM
тАО07-26-2006 03:39 PM
Re: Script question - merging files based on first parameter
awk -F"," '{
if(p[$1])
p[$1]=p[$1]","$2
else
p[$1]=$2
} END{for(i in p) print i","p[i]}' File-1 File-2 File-3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2006 05:03 PM
тАО07-26-2006 05:03 PM
Re: Script question - merging files based on first parameter
foreach $parm ( sort keys %lines ) {
$_ = join ",", @{$lines{$parm}};
chop;
print "$_\n";
}
I did not expect to need to chop the last ",", but it was needed on my windoze box
Mind you, that solution requires that all params are present for each node, and the params are re-ordered alphabetically, including the 'header' line.
Below is an alternative which tolerates missing values and keeps the order initially based on the first file processed.
Enable the '# print' in the middle to see what it is doing.
hth,
Hein.
use strict
my ($name,$value,$param,$params,$host,$hosts,$line,@data);
while (<>) {
chomp;
($name,$value) = split /,/;
next unless $name;
$hosts++ if /^Parameter,/;
$param = $names{$name};
if (!defined $param){
$names{$name} = ++$params;
$param = $params;
$data[$param][0] = $name;
}
$data[$param][$hosts] = $value;
# print "p=$param, h=$hosts, n=$name, v=$value\n";
}
foreach $param (1..$params) {
$_ = "";
foreach $host (0..$hosts) {
$_ .= $data[$param][$host].",";
}
chop;
print "$_\n";
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2006 07:23 PM
тАО07-26-2006 07:23 PM
Re: Script question - merging files based on first parameter
to get the output lines in order with Sandmans awk solution you have to add one additional array only:
awk -F, '{
if(p[$1])
p[$1]=p[$1]","$2
else {key[++j]=$1
p[$1]=$2
}
}
END{for(i=1;i<=j;i++) print key[i]","p[key[i]]}' files ...
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2006 01:05 AM
тАО07-27-2006 01:05 AM
SolutionTo add the commas to the output, simply change the line:
print "@{$lines{$parm}}";
...to:
print join ",", @{$lines{$parm}};
A simple hack to keeping the header line (sorted) to the top would be to enclose the first header field in angle brackets, e.g. for your File-1:
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-03-2006 07:54 AM
тАО08-03-2006 07:54 AM
Re: Script question - merging files based on first parameter
- Tags:
- join