<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Script question - merging files based on first parameter in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831578#M100607</link>
    <description>&lt;!--!*#--&gt;Hi:&lt;BR /&gt;&lt;BR /&gt;The following should serve your needs:&lt;BR /&gt;&lt;BR /&gt;# cat cmpkparm&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my %lines;&lt;BR /&gt;my ( $parm, $value );&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;    chomp;&lt;BR /&gt;    ( $parm, $value ) = split( /,/, $_ );&lt;BR /&gt;    if ( exists $lines{$parm} ) {&lt;BR /&gt;        push( @{ $lines{$parm} }, $value );&lt;BR /&gt;    }&lt;BR /&gt;    else {&lt;BR /&gt;        push( @{ $lines{$parm} }, $parm, $value );&lt;BR /&gt;    }&lt;BR /&gt;}&lt;BR /&gt;foreach $parm ( sort keys %lines ) {&lt;BR /&gt;    print "@{$lines{$parm}}";&lt;BR /&gt;    print "\n";&lt;BR /&gt;}&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;...Run as:&lt;BR /&gt;&lt;BR /&gt;# ./cmpkparm file1 file2 file3 ...&lt;BR /&gt;&lt;BR /&gt;That is, pass as many file names on the commandline as you need to process.  The format of each file is as you posted.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
    <pubDate>Wed, 26 Jul 2006 18:30:47 GMT</pubDate>
    <dc:creator>James R. Ferguson</dc:creator>
    <dc:date>2006-07-26T18:30:47Z</dc:date>
    <item>
      <title>Script question - merging files based on first parameter</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831577#M100606</link>
      <description>I have multiple files, here's the first few lines of each. &lt;BR /&gt;[ File-1 ]&lt;BR /&gt;Parameter,hostname1&lt;BR /&gt;NSTRBLKSCHED,-&lt;BR /&gt;NSTREVENT,50&lt;BR /&gt;NSTRPUSH,16&lt;BR /&gt;&lt;BR /&gt;[ File-2 ]&lt;BR /&gt;Parameter,hostname2&lt;BR /&gt;NSTRBLKSCHED,-&lt;BR /&gt;NSTREVENT,100&lt;BR /&gt;NSTRPUSH,26&lt;BR /&gt;&lt;BR /&gt;[ File-3 ]&lt;BR /&gt;Parameter,hostname3&lt;BR /&gt;NSTRBLKSCHED,-&lt;BR /&gt;NSTREVENT,150&lt;BR /&gt;NSTRPUSH,36&lt;BR /&gt;&lt;BR /&gt;etc - etc - etc&lt;BR /&gt;&lt;BR /&gt;I would like to read multiple input files, and if the first value in each line matches, output a single output line. &lt;BR /&gt;For example, from the above, I would get&lt;BR /&gt;&lt;BR /&gt;Parameter,hostname1,hostname2,hostname3&lt;BR /&gt;NSTRBLKSCHD,-,-,-&lt;BR /&gt;NSTREVENT,50,100,150&lt;BR /&gt;NSTRPUSH,16,26,36&lt;BR /&gt;&lt;BR /&gt;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 ?</description>
      <pubDate>Wed, 26 Jul 2006 17:21:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831577#M100606</guid>
      <dc:creator>S.Rider</dc:creator>
      <dc:date>2006-07-26T17:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: Script question - merging files based on first parameter</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831578#M100607</link>
      <description>&lt;!--!*#--&gt;Hi:&lt;BR /&gt;&lt;BR /&gt;The following should serve your needs:&lt;BR /&gt;&lt;BR /&gt;# cat cmpkparm&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my %lines;&lt;BR /&gt;my ( $parm, $value );&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;    chomp;&lt;BR /&gt;    ( $parm, $value ) = split( /,/, $_ );&lt;BR /&gt;    if ( exists $lines{$parm} ) {&lt;BR /&gt;        push( @{ $lines{$parm} }, $value );&lt;BR /&gt;    }&lt;BR /&gt;    else {&lt;BR /&gt;        push( @{ $lines{$parm} }, $parm, $value );&lt;BR /&gt;    }&lt;BR /&gt;}&lt;BR /&gt;foreach $parm ( sort keys %lines ) {&lt;BR /&gt;    print "@{$lines{$parm}}";&lt;BR /&gt;    print "\n";&lt;BR /&gt;}&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;...Run as:&lt;BR /&gt;&lt;BR /&gt;# ./cmpkparm file1 file2 file3 ...&lt;BR /&gt;&lt;BR /&gt;That is, pass as many file names on the commandline as you need to process.  The format of each file is as you posted.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Wed, 26 Jul 2006 18:30:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831578#M100607</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-07-26T18:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: Script question - merging files based on first parameter</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831579#M100608</link>
      <description>THANKS - but one dumb perl question.&lt;BR /&gt;How do I get the commas between the values outputed ?</description>
      <pubDate>Wed, 26 Jul 2006 20:22:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831579#M100608</guid>
      <dc:creator>S.Rider</dc:creator>
      <dc:date>2006-07-26T20:22:28Z</dc:date>
    </item>
    <item>
      <title>Re: Script question - merging files based on first parameter</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831580#M100609</link>
      <description>How about giving this awk construct a try:&lt;BR /&gt;&lt;BR /&gt;awk -F, '{if(p[$1])p[$1]=p[$1]","$2;else p[$1]=$2}END{for(i in p) print i,p[i]}' files&lt;BR /&gt;&lt;BR /&gt;cheers!</description>
      <pubDate>Wed, 26 Jul 2006 21:01:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831580#M100609</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-07-26T21:01:25Z</dc:date>
    </item>
    <item>
      <title>Re: Script question - merging files based on first parameter</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831581#M100610</link>
      <description>&lt;!--!*#--&gt;Matter of fact the code is easier to follow if indented and you can specify as many files as you like on the awk input line...so here goes:&lt;BR /&gt;&lt;BR /&gt;awk -F"," '{&lt;BR /&gt;   if(p[$1])&lt;BR /&gt;      p[$1]=p[$1]","$2&lt;BR /&gt;   else&lt;BR /&gt;      p[$1]=$2&lt;BR /&gt;} END{for(i in p) print i","p[i]}' File-1 File-2 File-3</description>
      <pubDate>Wed, 26 Jul 2006 22:39:02 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831581#M100610</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-07-26T22:39:02Z</dc:date>
    </item>
    <item>
      <title>Re: Script question - merging files based on first parameter</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831582#M100611</link>
      <description>You can fix JRF's solution to get commas by replacing the final printout with:&lt;BR /&gt;&lt;BR /&gt;foreach $parm ( sort keys %lines ) {&lt;BR /&gt;    $_ = join ",", @{$lines{$parm}};&lt;BR /&gt;    chop;&lt;BR /&gt;    print "$_\n";&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;I did not expect to need to chop the last ",", but it was needed on my windoze box&lt;BR /&gt;&lt;BR /&gt;Mind you, that solution requires that all params are present for each node, and the params are re-ordered alphabetically, including the 'header' line.&lt;BR /&gt; &lt;BR /&gt;&lt;BR /&gt;Below is an alternative which tolerates missing values and keeps the order initially based on the first file processed.&lt;BR /&gt;Enable the '# print' in the middle to see what it is doing.&lt;BR /&gt;&lt;BR /&gt;hth,&lt;BR /&gt;Hein.&lt;BR /&gt;&lt;BR /&gt;use strict&lt;BR /&gt;my ($name,$value,$param,$params,$host,$hosts,$line,@data);&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt; chomp;&lt;BR /&gt; ($name,$value) = split /,/;&lt;BR /&gt; next unless $name;&lt;BR /&gt; $hosts++ if /^Parameter,/;&lt;BR /&gt; $param = $names{$name};&lt;BR /&gt;  if (!defined $param){&lt;BR /&gt;  $names{$name} = ++$params;&lt;BR /&gt;  $param = $params;&lt;BR /&gt;  $data[$param][0] = $name;&lt;BR /&gt;  }&lt;BR /&gt;  $data[$param][$hosts] = $value;&lt;BR /&gt;# print "p=$param, h=$hosts, n=$name, v=$value\n";&lt;BR /&gt; }&lt;BR /&gt; foreach $param (1..$params) {&lt;BR /&gt;   $_ = "";&lt;BR /&gt;   foreach $host (0..$hosts) {&lt;BR /&gt;   $_ .= $data[$param][$host].",";&lt;BR /&gt;   }&lt;BR /&gt;   chop;&lt;BR /&gt;   print "$_\n";&lt;BR /&gt; }</description>
      <pubDate>Thu, 27 Jul 2006 00:03:00 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831582#M100611</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-07-27T00:03:00Z</dc:date>
    </item>
    <item>
      <title>Re: Script question - merging files based on first parameter</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831583#M100612</link>
      <description>&lt;!--!*#--&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;to get the output lines in order with Sandmans awk solution you have to add one additional array only:&lt;BR /&gt;&lt;BR /&gt;awk -F, '{&lt;BR /&gt;   if(p[$1])&lt;BR /&gt;      p[$1]=p[$1]","$2&lt;BR /&gt;   else {key[++j]=$1&lt;BR /&gt;      p[$1]=$2&lt;BR /&gt;  }&lt;BR /&gt;}&lt;BR /&gt;END{for(i=1;i&amp;lt;=j;i++) print key[i]","p[key[i]]}' files ...&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Thu, 27 Jul 2006 02:23:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831583#M100612</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2006-07-27T02:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: Script question - merging files based on first parameter</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831584#M100613</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;To add the commas to the output, simply change the line:&lt;BR /&gt;&lt;BR /&gt;print "@{$lines{$parm}}";&lt;BR /&gt;&lt;BR /&gt;...to:&lt;BR /&gt;&lt;BR /&gt;print join ",", @{$lines{$parm}};&lt;BR /&gt;&lt;BR /&gt;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:&lt;BR /&gt;&lt;BR /&gt;&lt;PARAMETER&gt;,hostname1&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;/PARAMETER&gt;</description>
      <pubDate>Thu, 27 Jul 2006 08:05:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831584#M100613</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-07-27T08:05:34Z</dc:date>
    </item>
    <item>
      <title>Re: Script question - merging files based on first parameter</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831585#M100614</link>
      <description>I would suggest possibly using the join command line command</description>
      <pubDate>Thu, 03 Aug 2006 14:54:31 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-question-merging-files-based-on-first-parameter/m-p/3831585#M100614</guid>
      <dc:creator>Thom Cornwell</dc:creator>
      <dc:date>2006-08-03T14:54:31Z</dc:date>
    </item>
  </channel>
</rss>

