溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

cut命令如何在linux中使用

發(fā)布時間:2021-04-01 16:12:20 來源:億速云 閱讀:100 作者:Leah 欄目:開發(fā)技術

cut命令如何在linux中使用?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

要用到,來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,確實很方便,下面簡單總結(jié)下:

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

1>截取第三個字符:

echo 123:456:789 | cut -c3
3

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

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

3>截取前三個字符

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

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

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

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

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

小結(jié)下

>>這個“-”比較有意思,

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

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

在兩個idx之間,表示從idx1到idx2。

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

比如要取第1,3,5,7個字符: 

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

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

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

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

cut比較小巧,在適當?shù)膱鼍跋率褂眯屎芨撸撬恢С终齽t表達式,所以在復雜的情況下還是使用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

關于cut命令如何在linux中使用問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI