Reputation: 1
I have a code sample that does not work at all. I permanently get an error produced by awk.
queried_num=$(echo -e "$domains"|awk '{print $(NF-1)}'|awk -F. '{print $(NF-2) "." $(NF-1)}'|wc -l)
queried_domain=$(echo -e "$domains"|awk '{print $(NF-1)}'|awk -F. '{print $(NF-2) "." $(NF-1)}'|uniq)
I don't really know what's going wrong and how to fix it.
The error is: awk: cmd. line:1: (FILENAME=- FNR=1) fatal: attempt to access field -1
Upvotes: 0
Views: 5575
Reputation: 203522
The 2 scripts you posted:
queried_num=$(echo -e "$domains"|awk '{print $(NF-1)}'|awk -F. '{print $(NF-2) "." $(NF-1)}'|wc -l)
queried_domain=$(echo -e "$domains"|awk '{print $(NF-1)}'|awk -F. '{print $(NF-2) "." $(NF-1)}'|uniq)
could be written as just something like:
queried_num=$(echo -e "$domains"|wc -l)
queried_domain=$(echo -e "$domains"|awk '{n=split($(NF-1),f,/./); $0=f[n-2] "." f[n-1]} !seen[$0]++')
and then to discard empty lines (which would cause the problem you describe) would be:
queried_num=$(echo -e "$domains"|grep '.'|wc -l)
queried_domain=$(echo -e "$domains"|awk '!NF{next} {n=split($(NF-1),f,/./); $0=f[n-2] "." f[n-1]} !seen[$0]++')
If that's not what you're looking for then edit your question to include some concise, testable sample input and expected output so we can help you.
Upvotes: 1