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
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
! 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.
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.