Comparing 2 files using awk , not getting any results - C shell

I am using c shell and trying to compare 2 files using awk . But the below awk statement doesnt give any result. Pls. advise why am not getting the desired o/p with the corrected awk script.
Need to acheive this solution in awk using C shell.

awk 'FNR==NR{a[$0]++;next} 
{for(i in a)
 {if ( a=$0 )
  {
   continue   
  else
     print $0
  }  
}' cmp1 cmp2 

cmp1 file

file:tst1
md5sum:aED567ZZZ
rowcount:1256

cmp2 file

file:tst1
md5sum:AED567ZZZ
rowcount:1257

o/p expected

rowcount:1257

the diff command is standard in any modern unix and does what you want, I think.
try:

diff cmp1 cmp2

And see if that will do what you need. It adds a little extra information so you can tell which file has what.

If you tell us your OS it might help - diff implementations may have extra options

You have several syntax and semantic errors in your script, like the usage of braces in the if construct, the usage of = for comparison in lieu of == , and the assumption a would contain a string equivalent to a former $0 (in fact, the index i is the string, a has a count value, 1 in above example). On top, the for loop will run across all three elements of a , printing the desired string thrice.

And, your expected output is not correct for the sample input files given (as "A" != "a" in line 2).

Try instead (pls be aware that proper indenting helps reading and understanding a code snippet tremendously):

awk '
FNR==NR {a[$0]++;
         next
        } 

!($0 in a)

'  file[12]
md5sum:AED567ZZZ
rowcount:1257
1 Like

As ! has a special meaning in C shell, will this work? Thinking we need to escape with \!($0 in a). Pls. advise.

How about using the below script , will this work in C shell?

awk '
 last=NR; 
	next
        }
{for(i=1;i<=last;i++)
 {
   if (a==$0)
   {
	continue
   else
     print $0
   }
 }' file[12]

! has a special meaning elsewhere in other systems. But. No "shell meaning" inside of a set of single tics, which is why the awk script must be set inside a pair of single tics, too.

You said C shell, and you really mean csh or tcsh?
C shell is nasty. It does history substiution even within ' ' and even if the shell is non-interactive or in scripts.
Within ' ' the only safe escape of the ! is '\!' (like the ' to be escaped as '\'' ).
Further, C shell does not take a multiline string. Try to put a \ at the end of each line.

Learn the standard shell, and recode your scripts! It is straight forward; the standard shell can do everything the C shell can.

2 Likes

I am using C shell. As per your advise i had this change will this work in C shell.

awk 'FNR==NR {a[$0]++; next} '\!''($0 in a)'  file[12]

Addl. queries.
-What do you mean by standard shell??
-What i see in echo $SHELL is /bin/csh . If we want to change the login shell and execute the same above command for it to work in the other compatible shells. Tell me what should be the syntax for it to work.
-If we need to sort the files, cmp1 and cmp2 before passing to the awk command , what should be the working command in C shell and other compatible shells

That's an awk script, independent of the shell running it. It will run flawlessly as given in post #3. DON'T change code unless knowing exactly what you are doing; why change it to a one-liner? Any errors?

The "standard shell" is the Bourne shell sh and its decendants like bash or ksh .
Try the chsh (change login shell) command, mayhap reading its man page upfront.
Can't talk for the C shell; try "process substitution" in e.g. bash . If that can't be deployed, work through intermediate files.

There was one ' too many.

awk 'FNR==NR {a[$0]++; next} '\!'($0 in a)'  file[12]

The shell sees 'string'\!'string' , and the awk sees string!string .

1 Like