顯示檔案中每行的指定範圍的列

2021-06-17 16:13:53 字數 1681 閱讀 3209

使用cut命令和選擇的組合來顯示檔案中每行的指定範圍的列

語法:cut [option]... [file]...

-f:顯示指定列或指定範圍的咧

-d:列之間的分隔符(預設是tab)

-c:顯示每行的第幾個字元到第幾個字元

1、使用-f顯示每行的第二列(分隔符是tab)

[root@localhost ~]# cat test.txt

first 192.168.0.1 no.301

first 192.168.0.1 no.301

first 192.168.0.1 no.301

first 192.168.0.1 no.301

first 192.168.0.1 no.301

first 192.168.0.1 no.301

first 192.168.0.1 no.301

first 192.168.0.1 no.301

[root@localhost ~]# cut -f2 test.txt

192.168.0.1

192.168.0.1

192.168.0.1

192.168.0.1

192.168.0.1

192.168.0.1

192.168.0.1

192.168.0.1

2、使用-f和-d顯示每行的第二列(分隔符是逗號)

[root@localhost ~]# cat test.txt

first,192.168.0.1,no.301

first,192.168.0.1,no.301

first,192.168.0.1,no.301

first,192.168.0.1,no.301

first,192.168.0.1,no.301

first,192.168.0.1,no.301

first,192.168.0.1,no.301

first,192.168.0.1,no.301

//-d後跟著的分隔符必須和檔名中間有空格

[root@localhost ~]# cut -f2 -d,test.txt

cut: the delimiter must be a single character

try `cut --help' for more information.

//正確

[root@localhost ~]# cut -f2 -d, test.txt

192.168.0.1

192.168.0.1

192.168.0.1

192.168.0.1

192.168.0.1

192.168.0.1

192.168.0.1

192.168.0.1

3、使用-c顯示每行指定區間的字元

[root@localhost ~]# cut -c3-21 test.txt

rst,192.168.0.1,no.

rst,192.168.0.1,no.

rst,192.168.0.1,no.

rst,192.168.0.1,no.

rst,192.168.0.1,no.

rst,192.168.0.1,no.

rst,192.168.0.1,no.

rst,192.168.0.1,no.

Linux head 顯示檔案的開頭

head 與tail 就像它的名字一樣的淺顯易懂,它是用來顯示開頭或結尾某個數量的文字區塊,head 用來顯示檔案的開頭至標準輸出中,而 tail 想當然爾就是看檔案的結尾。1 命令格式 head 引數 檔案 2 命令功能 head 用來顯示檔案的開頭至標準輸出中 預設head 命令列印其相應檔案的...

顯示檔案的命令彙總

cat 檔名 cat 檔案1 檔案2 檔案3 tac和cat命令相反,顛倒輸出順序,把尾部先顯示,頭部最後顯示。倒敘顯示。head 檔案 head 看檔案頭,預設10行 head n 20 檔案 顯示檔案頭20行 less 檔案1 more 檔案1 q 退出 tail 檔案 顯示檔案最後10行 ta...

history 顯示檔案和使用者的狀態

cvs 用乙個歷史日誌檔案來跟蹤大多數的 cvs 命令的使用。你可以用history以各種格式顯示此資訊。要開啟日誌功能,loghistory 配置選項必須設定為非空,而且 historylogpath 選項指定的歷史檔案,必須能被 cvs 指向的所有使用者寫入 參閱 config 要使用histo...