cut命令也是Linux下最常用的命令之一。
正如其名,cut的工作就是“剪”,具體的說就是在文件中負責剪切數據用的。cut是以每一行為一個處理對象的,這種機制和sed是一樣的。
cut命令的剪切度量有三種:
此外,cut命令還支持數字表示的范圍,
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
舉例子來說明一下:
按照字節剪切
date | cut -b 1-5
2014
date | cut -b 1-6
2014
date | cut -b 1-7
2014年
從上面的例子可以看出漢字“年”占了3個字節
按照字符剪切
date
2014年 10月 10日 星期五 14:48:16 CST
date | cut -c 1,3,4
214
多個定位之間用逗號隔開
按照域剪切
date
2014年 10月 10日 星期五 14:46:09 CST
date | cut -d" " -f 4,5
星期五 14:46:12
-d指示分割符 並且分割符必須是單字符。
10個有用的Linux命令面試問題及答案 http://www.linuxidc.com/Linux/2014-08/105150.htm
Linux命令之cd http://www.linuxidc.com/Linux/2013-05/83948.htm
Linux命令之cat http://www.linuxidc.com/Linux/2013-05/83947.htm
Linux命令之alias/unalias http://www.linuxidc.com/Linux/2013-05/83946.htm
Linux命令解析:su root和su -root http://www.linuxidc.com/Linux/2013-04/82564.htm
Linux命令之 交互式輸入read http://www.linuxidc.com/Linux/2013-03/81594.htm