This ought to be ridiculously easy. I simply want to print a single element of an array. However, all I get from a command like print arr[1] is an empty line.
Here is my entire bash script:
#!/bin/bash
find -X $1 -type f |
xargs md5 |
awk '
NF == 4 {
md5[$4]++;
files[$2]++;
}
END {
for (i = 1; i <= NF; i++)
for (j = i + 1; j <= NF; j++)
if (md5[i] == md5[j]) {
print "These are duplicates: "
print files[j+1]
print files[i]
}
'
exit 0
It is a very simply duplicate file finder. The problematic part is in the END{} statement within awk.
This just gives me a bunch of “These are duplicates: ” with empty lines after them. I know that the information is available, because I add this to END{}: for (x in arr); print x and it flawlessly prints every element in arr, as expected.
I must be doing something very silly.
What you’re currently doing is assigning the values you want to save as the indices of the two arrays, which seems to be common from code examples in
awk. However, that’s usually used in conjunction with thefor (x in y)syntax. To fix your code, the way that comes to mind to fix what you’re doing is to modify yourawkbit like so:And then change:
That should about do it, I think, but I haven’t tested it.