cut 命令:从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。
cut [-df] [file]
- -d :自定义分隔符,默认为制表符。
- -f :与-d一起使用,指定显示哪个区域。
# 将文件file.txt中每一行的内容以空格分隔,输出分隔后第5列的内容
cut -d ' ' -f5 file.txt
# 将文件file.txt中每一行的内容以空格分隔,输出分隔后第5列以及第5列之后的内容
cut -d ' ' -f5- file.txt
举个栗子:
文件file.txt内容如下:
2022/01/01 01:52:41.517 [D] [checkupdate.go:29] IP: 219.17.64.219 ,CurrVersion: VER-202201041149-001 ,DeviceId: 1a0fb929
2022/01/01 01:57:03.914 [D] [checkupdate.go:29] IP: 219.17.64.219 ,CurrVersion: VER-202201041149-001 ,DeviceId: 1a0fb929
2022/01/01 01:17:47.423 [D] [checkupdate.go:29] IP: 221.4.55.69 ,CurrVersion: VER-202201041149-001 ,DeviceId: 52e1c5f6
2022/01/01 01:41:56.312 [D] [checkupdate.go:29] IP: 59.17.252.215 ,CurrVersion: VER-202201101736-001 ,DeviceId: 6b97ca9f
2022/01/01 01:59:55.294 [D] [checkupdate.go:29] IP: 42.120.102.31 ,CurrVersion: VER-202201191134-52-001 ,DeviceId: 2ddc960c
2022/01/01 10:12:31.105 [D] [checkupdate.go:29] IP: 59.17.252.215 ,CurrVersion: VER-202201101736-001 ,DeviceId: 6b97ca9f
2022/01/01 11:21:32.596 [D] [checkupdate.go:29] IP: 60.126.101.50 ,CurrVersion: VER-202201041149-001 ,DeviceId: 6f2774df
2022/01/01 11:23:11.599 [D] [checkupdate.go:29] IP: 222.129.193.39 ,CurrVersion: VER-202201041149-001 ,DeviceId: b4bf7d6c
cut -d ' ' -f5 file.txt # 将文件file.txt中每一行的内容以空格分隔,输出分隔后第5列的内容
输出结果:
IP:
IP:
IP:
IP:
IP:
IP:
IP:
IP:
cut -d ' ' -f5- file.txt # 将文件file.txt中每一行的内容以空格分隔,输出分隔后第5列以及第5列之后的内容
输出结果:
IP: 219.17.64.219 ,CurrVersion: VER-202201041149-001 ,DeviceId: 1a0fb929
IP: 219.17.64.219 ,CurrVersion: VER-202201041149-001 ,DeviceId: 1a0fb929
IP: 221.4.55.69 ,CurrVersion: VER-202201041149-001 ,DeviceId: 52e1c5f6
IP: 59.17.252.215 ,CurrVersion: VER-202201101736-001 ,DeviceId: 6b97ca9f
IP: 42.120.102.31 ,CurrVersion: VER-202201191134-001 ,DeviceId: 2ddc960c
IP: 59.17.252.215 ,CurrVersion: VER-202201101736-001 ,DeviceId: 6b97ca9f
IP: 60.126.101.50 ,CurrVersion: VER-202201041149-001 ,DeviceId: 6f2774df
IP: 222.129.193.39 ,CurrVersion: VER-202201041149-001 ,DeviceId: b4bf7d6c