linux shell cut按列切分文件

linux cut按列切分文件
-f:選擇的哪些列
--complement 補集運算
例:
[root@localhost test]# cat student.data 
NO      Name    Mark
1       lufubo  98
2       cbiao   88
[root@localhost test]# cut -f2,3 student.data 
Name    Mark
lufubo  98
cbiao   88
[root@localhost test]# cut -f2,3 --complement student.data 
NO
1
2

如若要指定字段的定界符,使用-d選項:
[root@localhost test]# cat student.data 
NO;     Name;   Mark
1;      lufubo; 98
2;      cbiao;  88
[root@localhost test]# cut -f2 -d ";" student.data 
        Name
        lufubo
        cbiao

cut命令還可將一串字符作爲列來顯示:
N-:從第N個字節 ,字符到行尾
N-M:從第N-M個字節 
-M:從1-M個字節 

-b:表示字節
-c:表示字符
-f:表示定義字段
例 :
[root@localhost test]# cat data.txt 
asdfghjkl
asdfghjkl
asdfghjkl
[root@localhost test]# cut -c1-5 data.txt 
asdfg
asdfg
asdfg
[root@localhost test]# cut -c-2 data.txt 
as
as
as
[root@localhost test]# cut -b5 data.txt 
g
g
g

sed表達式常用單引號,不過也可用雙引號,雙引號會通過對表達式求值來對其進行擴展。當我們想在sed 表達式中使用一些變量字符時,雙引號就很有用了:
[root@localhost test]# test=hello
[root@localhost test]# echo hello world | sed "s/$test/HELLO/"
HELLO world

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章