濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > linux的cut命令用法總結(jié)

linux的cut命令用法總結(jié)

熱門標(biāo)簽:同花順電話機(jī)器人微信 湖北孝感如何辦理 威海電銷外呼系統(tǒng)好用嗎 武漢語音電銷機(jī)器人加盟 輝縣市地圖標(biāo)注 地圖標(biāo)注x是啥意思 北京電銷機(jī)器人對(duì)市場(chǎng)的影響 房產(chǎn)證地圖標(biāo)注的兩個(gè)面積 外呼系統(tǒng)的合法性

要用到,來mark一下:

ubuntu@VM-0-15-ubuntu:~/taoge$ cat b.txt 
abc
abcd
ubuntu@VM-0-15-ubuntu:~/taoge$ cat b.txt | cut -c 1
a
a
ubuntu@VM-0-15-ubuntu:~/taoge$ cat b.txt | cut -c 2
b
b
ubuntu@VM-0-15-ubuntu:~/taoge$ cat b.txt | cut -c 1-2
ab
ab
ubuntu@VM-0-15-ubuntu:~/taoge$ cat b.txt | cut -c 1-3
abc
abc
ubuntu@VM-0-15-ubuntu:~/taoge$ cat b.txt | cut -c 1-4
abc
abcd
ubuntu@VM-0-15-ubuntu:~/taoge$ cat b.txt | cut -c 1-5
abc
abcd
ubuntu@VM-0-15-ubuntu:~/taoge$ cat b.txt | cut -c 1-6
abc
abcd
ubuntu@VM-0-15-ubuntu:~/taoge$ 

常常配合awk使用。

cut命令可以按字節(jié),字符,域來截取字串,在某些情況下使用cut,確實(shí)很方便,下面簡(jiǎn)單總結(jié)下:

1.按字符截?。?源字串:123:456:789)

1>截取第三個(gè)字符:

echo 123:456:789 | cut -c3
3

2>截取第三到第六之間的字符:

echo 123:456:789 | cut -c3-6
3:45

3>截取前三個(gè)字符

echo 123:456:789 | cut -c-3
123

4>提取第三個(gè)及其后面的所有字符

echo 123:456:789 | cut -c3-
3:456:789

5>提取第三到第六和第八到第十間的字符

echo 123:456:789 | cut -c3-6,8-10
3:45:78

小結(jié)下

>>這個(gè)“-”比較有意思,

在inx前,表示從字串投開始,

放在inx后,表示從idx開始到字串末尾,

在兩個(gè)idx之間,表示從idx1到idx2。

>>還有這個(gè)“,”可以連接我們選擇的不連續(xù)的域,

比如要取第1,3,5,7個(gè)字符: 

echo 123:456:789 | cut -c1,3,5,7
1346

>>對(duì)于-b選項(xiàng)應(yīng)該和-c選項(xiàng)差不多吧,就是單位不同而已(我沒有像上面一樣測(cè)試,只是我的理解)

對(duì)于-d選項(xiàng)需要配合著-f選項(xiàng)使用,-d是用來指定分隔符,-f用來指定提取第幾個(gè)域的內(nèi)容

echo 123:456:789 | cut -d : -f 3
789

cut比較小巧,在適當(dāng)?shù)膱?chǎng)景下使用效率很高,但是它不支持正則表達(dá)式,所以在復(fù)雜的情況下還是使用awk或者sed比較好!

[xxx@~]$ cut --help

Usage: cut OPTION... [FILE]...
Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options too.
 -b, --bytes=LIST    select only these bytes
 -c, --characters=LIST  select only these characters
 -d, --delimiter=DELIM  use DELIM instead of TAB for field delimiter
 -f, --fields=LIST    select only these fields; also print any line
              that contains no delimiter character, unless
              the -s option is specified
 -n           (ignored)
   --complement    complement the set of selected bytes, characters
              or fields
 -s, --only-delimited  do not print lines not containing delimiters
   --output-delimiter=STRING use STRING as the output delimiter
              the default is to use the input delimiter
   --help   display this help and exit
   --version output version information and exit

Use one, and only one of -b, -c or -f.  Each LIST is made up of one
range, or many ranges separated by commas.  Selected input is written
in the same order that it is read, and is written exactly once.
Each range is one of:

  •   N     N'th byte, character or field, counted from 1
  •   N-    from N'th byte, character or field, to end of line
  •   N-M   from N'th to M'th (included) byte, character or field
  •   -M    from first to M'th (included) byte, character or field

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

您可能感興趣的文章:
  • 在linux下玩轉(zhuǎn)帶有超時(shí)時(shí)間的connect函數(shù)
  • 詳解linux下fsevents模塊引起的npm ls報(bào)錯(cuò)解決辦法
  • linux下搭建go環(huán)境的安裝配置講解
  • 詳解linux系統(tǒng)輸入輸出管理和vim的常用功能
  • linux shell之通過標(biāo)識(shí)測(cè)試文件系統(tǒng)屬性的方法示例
  • linux shell中if的各種判斷
  • linux shell之pushd、popd和dirs的使用講解
  • linux shell之控制臺(tái)打印各種顏色字體和背景的實(shí)現(xiàn)方法
  • linux下查看so或可執(zhí)行程序的依賴庫
  • linux中alarm函數(shù)的實(shí)例講解

標(biāo)簽:迪慶 武威 麗江 蚌埠 日喀則 安康 紹興 西寧

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux的cut命令用法總結(jié)》,本文關(guān)鍵詞  linux,的,cut,命令,用法,總結(jié),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《linux的cut命令用法總結(jié)》相關(guān)的同類信息!
  • 本頁收集關(guān)于linux的cut命令用法總結(jié)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    余江县| 新沂市| 贡嘎县| 邯郸县| 墨竹工卡县| 清苑县| 泸西县| 贵阳市| 建平县| 东乡族自治县| 仁布县| 青冈县| 遂平县| 庆阳市| 徐汇区| 城口县| 大渡口区| 虹口区| 肥东县| 蓝田县| 枞阳县| 凤凰县| 华坪县| 郧西县| 西华县| 贵阳市| 夏津县| 卫辉市| 广昌县| 依兰县| 眉山市| 昭平县| 张家港市| 延寿县| 巴里| 沈丘县| 光泽县| 金川县| 沁源县| 威海市| 瑞金市|