<?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: Compare two files with awk in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/compare-two-files-with-awk/m-p/4007650#M96230</link>
    <description>The challenge seems not as much a 'compare', but a 'join'.&lt;BR /&gt;&lt;BR /&gt;SQL is good at this stuff.&lt;BR /&gt;Perl is. Awk can do, but is more tedious.&lt;BR /&gt;&lt;BR /&gt;The proper solution depends a little on table size, but assuming table A is a few MB or less, try this:&lt;BR /&gt;&lt;BR /&gt;use strict;&lt;BR /&gt;my (%a_table, %duration, %counts);&lt;BR /&gt;open A, "&lt;A.TXT&gt;&lt;/A.TXT&gt;while (&lt;A&gt;) {&lt;BR /&gt;  my ($number,$name) = split;&lt;BR /&gt;  $a_table{$number}=$name;&lt;BR /&gt;  }&lt;BR /&gt;close A;&lt;BR /&gt;open B, "&lt;B.TXT&gt;&lt;/B.TXT&gt;while (&lt;B&gt;) {&lt;BR /&gt;  my ($x,$number,$duration,$status) = split;&lt;BR /&gt;  next unless $a_table{$number};&lt;BR /&gt;  $duration{$number.$status}+=$duration;&lt;BR /&gt;  $counts{$number.$status}++;&lt;BR /&gt;  }&lt;BR /&gt;close B;&lt;BR /&gt;foreach (sort keys %counts) {&lt;BR /&gt;  my $status = chop;&lt;BR /&gt;  printf ("%10s %5d %s %6d\n",&lt;BR /&gt;    $a_table{$_}, $counts{$_.$status}, $status, $duration{$_.$status});&lt;BR /&gt;  }&lt;BR /&gt;&lt;BR /&gt;Hein.&lt;BR /&gt;&lt;/B&gt;&lt;/A&gt;</description>
    <pubDate>Sat, 26 May 2007 10:22:14 GMT</pubDate>
    <dc:creator>Hein van den Heuvel</dc:creator>
    <dc:date>2007-05-26T10:22:14Z</dc:date>
    <item>
      <title>Compare two files with awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/compare-two-files-with-awk/m-p/4007649#M96229</link>
      <description>Hi All,&lt;BR /&gt; &lt;BR /&gt; Help needed for the following problem I have.&lt;BR /&gt; &lt;BR /&gt; I have two files:&lt;BR /&gt; FILE A&lt;BR /&gt; ========&lt;BR /&gt; 0113434343 SJJK:01&lt;BR /&gt; 0113434342 SJJK:01&lt;BR /&gt; 0113434346 SJJK:01&lt;BR /&gt; 0126652131 JGGG:01&lt;BR /&gt; 0126652133 JGGG:01&lt;BR /&gt; 0126652137 JGGG:01&lt;BR /&gt; 0214020000 BGBG:01&lt;BR /&gt; 0214020001 BGBG:01&lt;BR /&gt; 0214020002 BGBG:01&lt;BR /&gt;&lt;BR /&gt; FILE B&lt;BR /&gt; =======&lt;BR /&gt;258826955840     0113434343                        10 V      rjs+:01   MPO7DB&lt;BR /&gt;26465220052      0126652131                        70 V      rjs+:01   WKI7DB&lt;BR /&gt;258847585067     0730720260                       540 V      rjs+:01   VMO7DB&lt;BR /&gt;474892345        002215548734                    5570 V      rjs+:01   TSALOG&lt;BR /&gt;26771377193      0026391846469                   1600 V      rjs+:01   FTN7DB&lt;BR /&gt;258826953840     0113434343                        30 V      rjs+:01   MPO7DD&lt;BR /&gt;2348037059676    07313166266                        0 U      rjs+:01   BXF7CB&lt;BR /&gt;865966104371     0026391918459                   1210 V      rjs+:01   BE27TB&lt;BR /&gt;255555555550     0214020000                      1220 V      rjs+:01   AWP7CB&lt;BR /&gt;865966104370     0026391374035                      0 U      rjs+:01   BE27TB&lt;BR /&gt;255526953840     0113434343                        30 U      rjs+:01   MPO7BB&lt;BR /&gt;118346993        002348027245963                 8430 V      rjs+:01   TSALOG&lt;BR /&gt;255555555550     0214020000                         0 U      rjs+:01   AWP7CB&lt;BR /&gt;255526953840     0214020000                        20 V      rjs+:01   AWP7CB&lt;BR /&gt;DEF              0791407394                         0 U      rjs+:01   VLG7TB&lt;BR /&gt;&lt;BR /&gt;Use FILE A colum $1 to check in FILE B colum $2 and if true look at colum $4 if the colum is ="V",take &lt;BR /&gt;into consideration the $3. Continue the pattern match. If another match in $2 and $4 == "V" add the result &lt;BR /&gt;from the first match with $3 to this match as well as number of occurrences for e.g "2". If match $2 &lt;BR /&gt;and $4=="U" then add $3 and increment count=1.&lt;BR /&gt;&lt;BR /&gt;My Code So far&lt;BR /&gt;==============&lt;BR /&gt;awk 'FNR==NR {&lt;BR /&gt;        t[$1]=$2;f[$2]=0;next&lt;BR /&gt;        }&lt;BR /&gt;        $2 in t {&lt;BR /&gt;                if($4=="V"){&lt;BR /&gt;                print t[$2],++f[$2],$4&lt;BR /&gt;                }&lt;BR /&gt;                else&lt;BR /&gt;                        if($4=="U"){&lt;BR /&gt;                        print t[$2],++f[$2],$4&lt;BR /&gt;                        }&lt;BR /&gt;                }' First Second&lt;BR /&gt;&lt;BR /&gt;Output File should look like below&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;RESULT&lt;BR /&gt;======&lt;BR /&gt;NAME  COUNTS STATUS  DURATION&lt;BR /&gt;SJJK:01  2   V       40       (note this is for 0113434343)&lt;BR /&gt;SJJK:01  1   U       10     (note this is for 0113434343)&lt;BR /&gt;JGGG:01         1         V       70       (note this is for 0126652131)&lt;BR /&gt;BGBG:01         2         V     1240       (note this is for 0214020000)&lt;BR /&gt;BGBG:01         1         U        0       (note this is for 0214020000) &lt;BR /&gt;&lt;BR /&gt;Hope this helps&lt;BR /&gt;&lt;BR /&gt;Many Thanks&lt;BR /&gt;Chris</description>
      <pubDate>Sat, 26 May 2007 07:00:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/compare-two-files-with-awk/m-p/4007649#M96229</guid>
      <dc:creator>Chris Frangandonis</dc:creator>
      <dc:date>2007-05-26T07:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two files with awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/compare-two-files-with-awk/m-p/4007650#M96230</link>
      <description>The challenge seems not as much a 'compare', but a 'join'.&lt;BR /&gt;&lt;BR /&gt;SQL is good at this stuff.&lt;BR /&gt;Perl is. Awk can do, but is more tedious.&lt;BR /&gt;&lt;BR /&gt;The proper solution depends a little on table size, but assuming table A is a few MB or less, try this:&lt;BR /&gt;&lt;BR /&gt;use strict;&lt;BR /&gt;my (%a_table, %duration, %counts);&lt;BR /&gt;open A, "&lt;A.TXT&gt;&lt;/A.TXT&gt;while (&lt;A&gt;) {&lt;BR /&gt;  my ($number,$name) = split;&lt;BR /&gt;  $a_table{$number}=$name;&lt;BR /&gt;  }&lt;BR /&gt;close A;&lt;BR /&gt;open B, "&lt;B.TXT&gt;&lt;/B.TXT&gt;while (&lt;B&gt;) {&lt;BR /&gt;  my ($x,$number,$duration,$status) = split;&lt;BR /&gt;  next unless $a_table{$number};&lt;BR /&gt;  $duration{$number.$status}+=$duration;&lt;BR /&gt;  $counts{$number.$status}++;&lt;BR /&gt;  }&lt;BR /&gt;close B;&lt;BR /&gt;foreach (sort keys %counts) {&lt;BR /&gt;  my $status = chop;&lt;BR /&gt;  printf ("%10s %5d %s %6d\n",&lt;BR /&gt;    $a_table{$_}, $counts{$_.$status}, $status, $duration{$_.$status});&lt;BR /&gt;  }&lt;BR /&gt;&lt;BR /&gt;Hein.&lt;BR /&gt;&lt;/B&gt;&lt;/A&gt;</description>
      <pubDate>Sat, 26 May 2007 10:22:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/compare-two-files-with-awk/m-p/4007650#M96230</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2007-05-26T10:22:14Z</dc:date>
    </item>
  </channel>
</rss>

