I am concerned with only a portion of a particular field (hostname) in a log line and would like to print all the unique occurrences of said portion. I am currently doing something like:

    zcat $file | awk '$2~/f/ {print $14}' | sort | uniq -c | sort -rn

The problem is field 14 will be really long and contain a bunch of stuff like


Hostnames may vary and I want to print only the uniqe hostnames, sort them and count them, etc. How can I better structure my awk command to achieve this? Important to note everything prepending and appending the /hostname/ portion varies.

  • 1
    Will the hostname always be 5th slash-delimited field of $14? – glenn jackman Jun 18 '18 at 20:23

If hostnames are always between the 4th and the 5th / in the awk output, then this process will extract them:

cut -d / -f 5

Place it just after your original awk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.