
譬如说我有以下的多行:
user-bind static ip-address 10.199.27.246 mac-address 00d0-d0b6-c09a vlan 7 user-bind static ip-address 10.199.27.223 mac-address f0de-f127-e980 vlan 7 user-bind static ip-address 10.199.28.26 mac-address 3c97-0ecd-7797 vlan 7 user-bind static ip-address 10.199.27.192 mac-address 0027-19a3-516d vlan 7 user-bind static ip-address 10.199.29.199 mac-address 000f-e2d2-5a38 vlan 7 user-bind static ip-address 10.199.27.98 mac-address 0090-27b6-cd8c vlan 7 user-bind static ip-address 10.199.27.124 mac-address 047d-7bd4-1eaf vlan 7 ip 地址有 3 个网段: 10.199.27.0/24, 10.199.28.0/24, 10.199.29.0/24 我想排列成:
user-bind static ip-address 10.199.27.98 mac-address 0090-27b6-cd8c vlan 7 user-bind static ip-address 10.199.27.124 mac-address 047d-7bd4-1eaf vlan 7 user-bind static ip-address 10.199.27.192 mac-address 0027-19a3-516d vlan 7 user-bind static ip-address 10.199.27.223 mac-address f0de-f127-e980 vlan 7 user-bind static ip-address 10.199.27.246 mac-address 00d0-d0b6-c09a vlan 7 user-bind static ip-address 10.199.28.26 mac-address 3c97-0ecd-7797 vlan 7 user-bind static ip-address 10.199.29.199 mac-address 000f-e2d2-5a38 vlan 7 使用 cut 、 sort 、 awk 、 sed 能实现吗?
1 ldbC5uTBj11yaeh5 2017-04-14 15:52:04 +08:00 cat 1.txt | sort -h |
3 ioiioi OP 抱歉,我用的是 babun on windows , sort -h 是起什么作用? |
4 ldbC5uTBj11yaeh5 2017-04-14 16:06:56 +08:00 |
5 ioiioi OP 我现在的方法比较蠢: ``` cat raw.txt | cut -d " " -f 4 > ip.list.txt cat ip.list.txt | sort -t . -k 3,3n -k 4,4n > ip.list.sorted.txt # <1> ``` <1> https://www.cyberciti.biz/faq/unix-linux-shell-script-sorting-ip-addresses/ 得到的结果只是一串排序后的 ip ,剩余字符串就全部丢失了。 |
6 Glink 2017-04-14 16:47:38 +08:00 cat test.txt |sort -t. -k 3,3n -k4,4n 直接这么写就可以了 |
7 gouchaoer 2017-04-14 16:50:08 +08:00 把 ip 转化成 unsigned int ,然后快拍。。。。为啥一定要用难用的 sort cut 之类的,用熟悉的语言就 ok 了 |
8 ioiioi OP @Glink wow !我直接用了 sort -t . -k 3,3n -k 4,4n source.txt > output.txt 不过,假如有一些行的 ip 地址之前包含了“.”,而有一些行又没有的话,又该怎么处理?因为无法用-k 3 来限定排序的字段了。 我想找一个比较通用的处理办法。 |
10 ldbC5uTBj11yaeh5 2017-04-14 17:06:31 +08:00 哦,那可以试下这个 cat 1.txt | awk '{print $4" "$0}' | sort -t. -k3,3n -k4,4n | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' |
11 realpg PRO EXCEL 大法好…… |
15 ioiioi OP @jigloo 我得好好消化一下,如果你能简答说明下思路更好,我大概知道 awk '{print $4" "$0}'是为了截取 ip 地址, sort 是为了排序,但是后面那个 awk 就完全不知道是干嘛了。 |
16 ldbC5uTBj11yaeh5 2017-04-14 17:46:50 +08:00 @ioiioi 后面做繁琐了,这个更清楚点。 cat 1.txt | awk '{print $4 $0}' | sort -t. -k3,3n -k4,4n | awk '{sub($1 FS,""); print}' 你先不要执行整个,把这几个管道一个个加上,然后在执行,就能清楚这个过程了。 |
17 realpg PRO |
20 lilydjwg 2017-04-14 18:31:27 +08:00 其实你可以用 piep ,方便好用又容易理解: piep -e 'from socket import inet_aton' 'pp.sortby(fn=lambda x: inet_aton(x.split()[3]))' < input piep 的文档: http://gfxmonk.net/dist/doc/piep/ |
22 gouchaoer 2017-04-14 19:00:25 +08:00 via Android awk sed sort 那么难,去学它的语法用法干嘛 |
24 ioiioi OP |
25 realpg PRO |
26 ioiioi OP @realpg 嗯,今早上试了一下, excel 有个分列的功能,但是 ip 地址的排序似乎有点问题,只能排成: ``` 10.199.27.124 10.199.27.192 10.199.27.223 10.199.27.246 10.199.27.98 10.199.28.26 10.199.29.199 ``` 有什么解决的办法没? |
27 itsjoke 2017-04-15 12:02:53 +08:00 这绑定有意义么? 如果主机多了那不得写死了,不是有免费 ARP 吗? |
29 runntuu 2017-04-15 15:49:51 +08:00 @ioiioi #26 对 ip 地址进行分割,然后对 ip 地址的第 3 段和第 4 段“自定义排序“ - ”升序”排序 如果是解决问题的话,用 excel 这样做确实方便,倒是感觉很不 geek = = |
30 ioiioi OP @itsjoke 怎么解决这个问题?我知道 dhcp snooping 可以防止客户私设 dhcp ,但是如何方式客户手工配置 ip 导致的 ip 地址冲突? |
31 ioiioi OP @realpg 我就是把 ip 地址单独 copy 出来,但是排序的时候, excel 会将.1xx 排在.9x 之前,实际上.98 应该要排在第一的。 |