日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux cut用法

發布時間:2025/3/16 linux 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux cut用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、作用

cut命令是一個選取命令,其功能是將文件中的每一行”字節” ”字符” ”字段” 進行剪切,選取我們需要的,并將這些選取好的數據輸出至標準輸出

二、格式

cut -[n]b file?
cut -c file?
cut -d[分隔符] -f[域] file

三、參數解釋

-b(bytes) :以字節為單位進行分割。這些字節位置將忽略多字節字符邊界,除非也指定了 -n 標志。?
-c(characters) :以字符為單位進行分割。?
-d :自定義分隔符,默認為制表符。?
-f(filed) :與-d一起使用,指定顯示哪個區域。?
-n :取消分割多字節字符。僅和 -b 標志一起使用。如果字符的最后一個字節落在由 -b 標志的 List 參數指示的
范圍之內,該字符將被寫出;否則,該字符將被排除。

四、實例分析

新建一個test1.txt,如下

557adfhg bcd5464b 135465453456 233546576
新建一個test2.txt,如下

星期一 星期二 星期三 星期四 星期五 星期六 星期日

1) -b

1.剪切單個字節

如下,只剪切txt中的每一行的第一個字節

[root@localhost shell]# cut -b 1 test1.txt 5 b 1 2 [root@localhost shell]#

2.剪切多個字節

剪切多個字符有很多方式,?
如 -b 1,3,5 //剪切每一行第 1 3 5個字符 (示例1)?
如 -b 1-5 //剪切每一行第 1-5 個字符 (示例2)?
如 -b -5 //剪切每一行第 1-5 個字符 (示例3)?
如 -b 3- //剪切每一行第 3個字符以后的 (示例4)

示例1:

[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進行操作,看有什么現象

[root@localhost shell]# cut -b 2 test2.txt � � � � � � � [root@localhost shell]#
出現了亂碼的現象,因為-b 只是針對字節進行裁剪,對一個漢字進行字節裁剪,得到的結果必然是亂碼,若想使用 -b 命令對字節進行裁剪,那么則需要使用 -n 選項,此選項的作用是取消分割多字節字符。

[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 只是針對于格式固定的數據中剪切,但是對于一些格式不固定的,就沒有辦法獲取到我們想要的數據,因此便有了 -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 命令,指定其分割符為 : 然后再選取第一個域內的內容即可,如下

[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 //以 : 為分隔符,選取第二個域里面的內容,輸出 [root@localhost shell]# ifconfig eth0 | grep "inet addr" | cut -d : -f 2 | cut -d ' ' -f 1 192.168.1.199 //以空格為分割符,選取第一個域內的內容,輸出 [root@localhost shell]#


4)實現對字符串的分割

我們有這樣一個字符串:

info='abcd;efgh'


現在想獲取abcd和efgh,我們可以簡單地用cut工具來獲取:
fstr=`echo $info | cut -d ; -f 1` sstr=`echo $info | cut -d ; -f 2`

總結

以上是生活随笔為你收集整理的Linux cut用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。