0

Desired Matching Key: $4$3$2 from line 01 and $4 from line 07 of File1.txt

Example: In File1.txt $4=5000, $3=68, $2=89 in line 01 and $4=RT0429 or RT0428 or RT0588 in line 07. The matching key in File2.txt is 50006889RT0428

Issue: I am looking to create a match key from File1.txt and match against File2.txt first 14 characters.

File1.txt (input)

01  89  68  5000
02  83  11
04  83  9   02
03  83  00
06  83  00
07  83  11  RT0429
07  83  09  RT0428
07  88  10  RT0588
01  44  73  8800
02  44  73
04  44  73   02
03  44  73
06  44  73
07  44  11  RT0789

File2.txt (input)

50006889RT0428 CCARD /3010  /E     /C A87545457          /  //                ///11        ///

51002387 CCARD /3000  /E     /S N054896334IV          /  //                ///11        ///

51002390800666 CCARD /3000  /E     /S N0978898IV          /  //                ///11        ///

File3 (Desired Output) Since only the first record from File1.txt has the matching key, the output would have the matching record from File2.txt

50006889RT0428 CCARD /3010  /E     /C A87545457          /  //                ///11        ///

Script I am using

awk '
  BEGIN {
    OFS="\t"
    out = "File3.txt"
    err = "File4.txt"
  }
  NR==FNR && NF {line[$1]=$0; next}
  function print_77_99() {
    if (key in line) 
      print "77", line[key] > out
    else {
      print "99", date > out
      printf "%s", lines >> err
    }
  }
  $1 == "01" {
    if (FNR > 1) print_77_99()
    key = $4 $3 $2
    lines = ""
  }
  {
    print > out
    lines = lines $0 "\n"
  }
  END {print_77_99()}
' File2.txt File1.txt
HighTech
  • 25
  • 7
  • What part of this are you stuck on specifically? – Etan Reisner Jan 26 '15 at 15:53
  • You may at least reference your previous question [here](http://stackoverflow.com/questions/28058805/awk-combine-the-data-from-2-files-and-print-to-3rd-file-if-keys-matched) as it's only a small adaptation from it (just remove the print which are not the 77 and remove the 77 from the printf ...) – Tensibai Jan 26 '15 at 16:07
  • @EtanReisner I have updated the question and added the script I am using. I want to create a key that derives $4$3$2 from Line 01 and $4 from Line 07 from File1.txt and match against the first 14 characters of File2.txt. Currently my script is only matching the $4$3$2 from Line 01 from File1.txt only, not $4 from Line 07. Thanks, – HighTech Jan 26 '15 at 18:02

1 Answers1

0

As per reading my comment it sounds unclear to me, here what I think would do:

(Code adapted from Jonathan leffler's answer in this question

FNR == NR && ! /^[[:space:]]*$/ { 
    key = substr($1, 1, 8)
    a[key] = $0
    next
}
$1 == "01" { if (code != 0)
             {
                 if (code in a)
                 {
                     print a[code]
                     delete a[code]
                 }
             }
             code = $4$3$2
           }
END {
         if (code in a)
         {
             print a[code]
             delete a[code]
         }
    }
Community
  • 1
  • 1
Tensibai
  • 15,557
  • 1
  • 37
  • 57
  • Thanks for your reply. I am not using the script from Jonathan. I have updated the question. But currently the script is taking only $4$3$2 form line 01 only, not $4 from Line 07. – HighTech Jan 26 '15 at 17:51
  • @Etan Reisner I have updated the question and added the script I am using. I want to create a key that derives $4$3$2 from Line 01 and $4 from Line 07 from File1.txt and match against the first 14 characters of File2.txt. Currently my script is only matching the $4$3$2 from Line 01 from File1.txt only, not $4 from Line 07. Thanks,\ – HighTech Jan 26 '15 at 17:55