Linux cut用法
一、作用
cut命令是一個選取命令,其功能是將文件中的每一行”字節(jié)” ”字符” ”字段” 進(jìn)行剪切,選取我們需要的,并將這些選取好的數(shù)據(jù)輸出至標(biāo)準(zhǔn)輸出
二、格式
cut -[n]b file?
cut -c file?
cut -d[分隔符] -f[域] file
三、參數(shù)解釋
-b(bytes) :以字節(jié)為單位進(jìn)行分割。這些字節(jié)位置將忽略多字節(jié)字符邊界,除非也指定了 -n 標(biāo)志。?
-c(characters) :以字符為單位進(jìn)行分割。?
-d :自定義分隔符,默認(rèn)為制表符。?
-f(filed) :與-d一起使用,指定顯示哪個區(qū)域。?
-n :取消分割多字節(jié)字符。僅和 -b 標(biāo)志一起使用。如果字符的最后一個字節(jié)落在由 -b 標(biāo)志的 List 參數(shù)指示的
范圍之內(nèi),該字符將被寫出;否則,該字符將被排除。
四、實(shí)例分析
新建一個test1.txt,如下
557adfhg bcd5464b 135465453456 233546576新建一個test2.txt,如下
星期一 星期二 星期三 星期四 星期五 星期六 星期日
1) -b
1.剪切單個字節(jié)
如下,只剪切txt中的每一行的第一個字節(jié)
[root@localhost shell]# cut -b 1 test1.txt 5 b 1 2 [root@localhost shell]#2.剪切多個字節(jié)
剪切多個字符有很多方式,?
如 -b 1,3,5 //剪切每一行第 1 3 5個字符 (示例1)?
如 -b 1-5 //剪切每一行第 1-5 個字符 (示例2)?
如 -b -5 //剪切每一行第 1-5 個字符 (示例3)?
如 -b 3- //剪切每一行第 3個字符以后的 (示例4)
[root@localhost shell]# cut -b 1,3,5 test1.txt 57d bd4 156 234 [root@localhost shell]#
示例2:
[root@localhost shell]# cut -b 1-5 test1.txt 557ad bcd54 13546 23354 [root@localhost shell]#
示例3: [root@localhost shell]# cut -b -5 test1.txt 557ad bcd54 13546 23354 [root@localhost shell]#
示例4:
[root@localhost shell]# cut -b 3- test1.txt 7adfhg d5464b 5465453456 3546576 [root@localhost shell]#
3.剪切字符
首先按照上面的例子對test2.txt進(jìn)行操作,看有什么現(xiàn)象
[root@localhost shell]# cut -b 2 test2.txt � � � � � � � [root@localhost shell]#出現(xiàn)了亂碼的現(xiàn)象,因?yàn)?b 只是針對字節(jié)進(jìn)行裁剪,對一個漢字進(jìn)行字節(jié)裁剪,得到的結(jié)果必然是亂碼,若想使用 -b 命令對字節(jié)進(jìn)行裁剪,那么則需要使用 -n 選項(xiàng),此選項(xiàng)的作用是取消分割多字節(jié)字符。
[root@localhost shell]# cut -nb 3 test2.txt 星 星 星 星 星 星 [root@localhost shell]# cut -nb 3,6 test2.txt 星 星期 星期 星期 星期 星期 星期 [root@localhost shell]# cut -nb 3,6,9 test2.txt 星期 星期二 星期三 星期四 星期五 星期六 星期日 [root@localhost shell]# cut -nb 3,6,9,12 test2.txt 星期一 星期二 星期三 星期四 星期五 星期六 星期日 [root@localhost shell]#
2) -c
-c的作用就是剪切字符,和上面的 -nb 有些類似
[root@localhost shell]# cut -c 1 test2.txt 星 星 星 星 星 星 [root@localhost shell]# cut -c 2 test2.txt 星 期 期 期 期 期 期 [root@localhost shell]# cut -c 1-3 test2.txt 星期 星期二 星期三 星期四 星期五 星期六 星期日 [root@localhost shell]#3)-f
上面的-b -c 只是針對于格式固定的數(shù)據(jù)中剪切,但是對于一些格式不固定的,就沒有辦法獲取到我們想要的數(shù)據(jù),因此便有了 -f 域的概念。
示例1:
[root@localhost shell]# cat /etc/passwd | head -n 3 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin [root@localhost shell]#例如將上面的第一個 : 前面的字符給剪切出來,那么我們就可以使用 -d 命令,指定其分割符為 : 然后再選取第一個域內(nèi)的內(nèi)容即可,如下
[root@localhost shell]# cat /etc/passwd | head -n 3 | cut -d : -f 1 root bin daemon [root@localhost shell]#
示例2:?
剪切ip地址,如下:
[root@localhost shell]# ifconfig eth0 | grep "inet addr"inet addr:192.168.1.199 Bcast:192.168.1.255 Mask:255.255.255.0 [root@localhost shell]# ifconfig eth0 | grep "inet addr" | cut -d : -f 2 192.168.1.199 Bcast //以 : 為分隔符,選取第二個域里面的內(nèi)容,輸出 [root@localhost shell]# ifconfig eth0 | grep "inet addr" | cut -d : -f 2 | cut -d ' ' -f 1 192.168.1.199 //以空格為分割符,選取第一個域內(nèi)的內(nèi)容,輸出 [root@localhost shell]#
4)實(shí)現(xiàn)對字符串的分割
我們有這樣一個字符串:
info='abcd;efgh'
fstr=`echo $info | cut -d ; -f 1` sstr=`echo $info | cut -d ; -f 2`
總結(jié)
以上是生活随笔為你收集整理的Linux cut用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux bash逐行读取文件的方法
- 下一篇: package-lock.json 文件