Quick Linux Tip #19:
Try: ss -tn state established | awk 'NR>1 {split($3,a,":"); print a[1]}' | sort -u
Info: First run ss to see the raw output, then use awk to parse it. split($3,a,":") splits IP:PORT into array a. NR>1 skips the header line.
Examples:
- $ ip -4 addr | awk '/inet / && !/127.0/ {print $2}' | cut -d/ -f1
- $ netstat -tn | awk 'NR>2 {split($4,a,":"); print a[1]}' | sort -u
- $ cat access.log | awk '{print $1}' | sort -u # Unique visitor IPs
Note: awk is a full text-processing language. $N is the Nth column, NR is the line number, and split() breaks strings into arrays.
Leave a Reply