I know there are "sort" and "uniq" out there, however, today's question is about how to utilise AWK to do that kind of a job. Say if I have a list of anything really (ips, names, or numbers) and I want to sort them;
Here is an example I am taking the IP numbers from a mail log:
awk 'match($0,/\[[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\]/) { if ( NF == 8 && $6 == "connect" ) {print substr($0, RSTART+1,RLENGTH-2)} }' maillog
Is it possible to sort them, ips, "on the go" within the same awk command? I do not require a complete answer to my question but some hints where to start.
Cheers!
!a[$0]++
is not equal touniq
, becauseuniq
requires input data sorted. You needawk 'l != $0 {l = $0}'
. – cuonglm Mar 30 '15 at 08:53uniq
has a couple behaviours that you can choose depending on the options. But the OP asked for something "to do that kind of a job"; i.e. how to accomplish that in principle withawk
. The method I presented is in this respect more powerful since its logic operates over all data in the file. This is on Unix typically reflected bysome-process | sort | uniq
, or (with a different semantic) bysome-process | sort -u
. Again, this is also not "equal", but you rarely want to imitate a Unix command, but rather solve a conncrete task. – Janis Mar 30 '15 at 08:59sort
? I think I'm running into a quoting issue using this code:awk 'NR=1; {print $0 | "sort -grk6 -t $'\t'"}' <filename>
, which gives the errorsort: option requires an argument -- 't'
. Thanks. – Josh Apr 07 '20 at 14:53sort -u
doesn't work on the latest version of git for windows.awk ... | sort -u
error-uThe system cannot find the file specified.
If the answer is yes, then I'll be honest, I don't think that this answer explains it very well at all. – xenoterracide Mar 16 '21 at 17:33