我们举一个简单的例子(非全部示例)来描述cut有什么用,可以做什么
比如这个日志,可能会出现折行,另外假设04-19 18:26:55.605 22750 22883 W
这些列的信息属于干扰信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 04-19 18:26:55.605 22750 22883 W System.err: java.lang.NoSuchFieldException 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.au.a(Unknown Source:16) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.au.a(Unknown Source:4) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.z.a(Unknown Source:880) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.z.a(Unknown Source:188) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.p.a(Unknown Source:821) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.p.a(Unknown Source:605) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.p.a(Unknown Source:11) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.d.a(Unknown Source:46) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.ak.b(Unknown Source:50) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.ak.a(Unknown Source:10) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.XMPushService$c.a(Unknown Source:8) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.XMPushService$i.run(Unknown Source:37) 04-19 18:26:55.605 22750 22883 W System.err: at com.xiaomi.push.service.g$c.run(Unknown Source:175) 04-19 18:27:24.787 23660 23685 W System.err: load vdr indoor lib success. |
---|
使用cut 我们可以实现删除上面的冗余信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | adb logcat | grep "System.err" --line-buffered | cut -d " " -f 6- System.err: java.lang.NoSuchFieldException System.err: at com.xiaomi.push.au.a(Unknown Source:16) System.err: at com.xiaomi.push.au.a(Unknown Source:4) System.err: at com.xiaomi.push.service.z.a(Unknown Source:880) System.err: at com.xiaomi.push.service.z.a(Unknown Source:188) System.err: at com.xiaomi.push.service.p.a(Unknown Source:821) System.err: at com.xiaomi.push.service.p.a(Unknown Source:605) System.err: at com.xiaomi.push.service.p.a(Unknown Source:11) System.err: at com.xiaomi.push.service.d.a(Unknown Source:46) System.err: at com.xiaomi.push.service.ak.b(Unknown Source:50) System.err: at com.xiaomi.push.service.ak.a(Unknown Source:10) System.err: at com.xiaomi.push.service.XMPushService$c.a(Unknown Source:8) System.err: at com.xiaomi.push.service.XMPushService$i.run(Unknown Source:37) System.err: at com.xiaomi.push.service.g$c.run(Unknown Source:175) |
---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # 取第一个字节 [email protected]:~ $ echo "abcdef" | cut -b 1 a # 取第二个字节 [email protected]:~ $ echo "abcdef" | cut -b 2 b # 从1开始,0会报错 [email protected]:~ $ echo "abcdef" | cut -b 0 cut: byte/character positions are numbered from 1 Try 'cut --help' for more information. |
---|
1 2 3 4 5 6 7 8 9 10 11 | #1到2字节位置切分 [email protected]:~ $ echo "abcdef" | cut -b 1-2 ab # 限定区间起始位置,不限定结束位置 [email protected]:~ $ echo "abcdef" | cut -b 1- abcdef # 限定区间结束位置,但不限定起始位置 [email protected]:~ $ echo "abcdef" | cut -b -5 abcde |
---|
1 2 | [email protected]:~ $ echo "abcdef" | cut -b 1,3,5 ace |
---|
当我们按照字符进行切分时,会遇到一些问题,比如出现中文的时候(一个中文占用三个字节)
1 2 3 4 5 6 7 | # 异常出现 echo "小黑屋" | cut -b 1 � ##必须限定满足正确的开始和结束位置 echo "小黑屋" | cut -b 1-3 小 |
---|
但是如果中英文并存,就比较麻烦了,好在有按照字符切分的方法(cut -c)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | echo "abcd技术小黑屋ef" | cut -c 7 小 echo "abcd技术小黑屋ef" | cut -c 7-9 小黑屋 echo "abcd技术小黑屋ef" | cut -c 7,8,9 小黑屋 echo "abcd技术小黑屋ef" | cut -c 5- 技术小黑屋ef echo "abcd技术小黑屋ef" | cut -c -9 abcd技术小黑屋 |
---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | echo "A|BC|DEF|GHIJ" | cut -d "|" -f 1 A echo "A|BC|DEF|GHIJ" | cut -d "|" -f 2 BC echo "A|BC|DEF|GHIJ" | cut -d "|" -f 3 DEF # 区间,限定开始位置 echo "A|BC|DEF|GHIJ" | cut -d "|" -f 1- A|BC|DEF|GHIJ #区间,限定结束位置 echo "A|BC|DEF|GHIJ" | cut -d "|" -f -3 A|BC|DEF # 区间,限定开始和结束位置 echo "A|BC|DEF|GHIJ" | cut -d "|" -f 1-2 A|BC #提供多个位置 echo "A|BC|DEF|GHIJ" | cut -d "|" -f 1,2 A|BC |
---|
cut -b 1 test_cut_file.txt
,cut -c 1 test_cut_file.txt
,cut -d "|" -f 1 test_cut_file.txt