Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK question

Status
Not open for further replies.

bholav

Technical User
Jun 13, 2002
15
US
I have two files that read like below.
a. I need to create a third file that is output in the same format as the two files after comapring the individual fields ,
for example comparison $3 of file1 is 7 and file2 is 1. Since they are not equal I'd like to output a "1" if not a "X" on file3




file1 : 1 1 7 7 1 7 1 1 7 1 1 7 1 1 7 7 1 7 1 7 7 1 7 1 1 7 1 1 1 1 1 1

file2: 1 1 1 1 1 1 7 1 7 7 1 1 7 1 7 7 7 7 1 1 7 1 7 7 1 1 1 1 1 1 1 1
 
I'm not sure when you want to print a 1 and when you want to print an X, but this should get you started.
Code:
{
  if ((getline a < &quot;f2.dat&quot;) <= 0) {
    print &quot;error&quot;
    exit
  }
  n2 = split(a, a2, &quot; &quot;)
  if (n2 != NF) {
    print &quot;error&quot;
    next
  }
  for (j=1;j<=NF;j++) {
    if ($j==a2[j]) {
      printf $j &quot; &quot; # or printf &quot;1 &quot; ?
    }
    else {
      printf &quot;X &quot;
    }
  }
  print &quot;&quot;
}
Hope this helps. CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top