0

In bash shell I'm trying to sort by a numeric value at the end of each line (sort by number of packets) Tried to use "sort -k6 -t '=' -n but not working.

Input:

type udp ip src=192.168.2.173 dport=53 packets=1
type udp ip src=192.168.2.168 dport=53 packets=1
type udp ip src=192.168.2.173 dport=53 packets=1
type udp ip src=192.168.2.170 dport=53 packets=560
type udp ip src=192.168.2.173 dport=53 packets=1
type udp ip src=192.168.2.175 dport=53 packets=1
type udp ip src=192.168.2.173 dport=53 packets=11
type udp ip src=192.168.2.178 dport=53 packets=1
type udp ip src=192.168.2.162 dport=53 packets=23
type udp ip src=192.168.2.173 dport=53 packets=75
type udp ip src=192.168.2.173 dport=53 packets=7
type udp ip src=192.168.2.173 dport=53 packets=100
type udp ip src=192.168.2.173 dport=53 packets=1
type udp ip src=192.168.2.173 dport=53 packets=200
type udp ip src=192.168.2.162 dport=7777 packets=75

1 Answer 1

2

You chose = as the field separator. In each line there are exactly three separators, so the "numeric value at the end of each line" is the field number 4.

These are the fields:

type udp ip src=192.168.2.162 dport=7777 packets=75
111111111111111 2222222222222222222 333333333333 44

Therefore:

sort -k4 -t '=' -n

Alternatively you can choose space as the separator and then the field will be indeed 6, but you need to tell sort to start at the 9th character of the field.

type udp ip src=192.168.2.162 dport=7777 packets=75
1111 222 33 44444444444444444 5555555555 6666666666
                                         123456789…

So this will also work:

sort -k6.9 -t ' ' -n
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .