Last edit
Changed:
< * If you want to compare two files with awk (doesn't work if {{{file1}}} is empty):
to
> * If you want to compare two files with awk (doesn't work if {{{file1}}} is empty).
> The following code snippet passes an array via {{{file1}}}:
awk -v list='foo,bar,baz' ' BEGIN { n=split(list, array, /,/) # now: array[1] == "foo", array[2] == "bar", ... for (idx in array) map[array[idx]] = k } $0 in map { ... }'
file1
is empty). The following code snippet passes an array via file1
:awk ' # cmp as awk program NR == FNR { array[NR] = $0; next } !(FNR in array && $0 == array[FNR]) { result = 1; exit } END { exit (NR != 2 * FNR || result + 0) } ' file1 file2
With gawk one could use ARGIND == 1
instead of NR == FNR
, which is working also for an empty file file1
.
For an explanation of this technique see: ComparingTwoFiles