linux cut 命令(转)
生活随笔
收集整理的這篇文章主要介紹了
linux cut 命令(转)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個非常有用的命令,主要用來提取各種各樣的數據。
cut -cchars file
如:
? ? -c5? ???提取第5個字符
? ?? ???-c5-? ? 提取第5個字符以后的字符
? ?? ???-c1,5,12??提取多個字符,中間用“,”符號隔開
? ?? ???-c5-14??提取第5個字符到第14個字符間的字符
http://gan.cublog.cn
[service@dsg tmp]$ cat f.txt
service??pts/0? ?? ???Oct??9 20:27 (211.95.114.235)
service??pts/1? ?? ???Oct??9 21:06 (218.80.203.242)
service??pts/2? ?? ???Oct??9 14:35 (218.80.203.242)
service??pts/3? ?? ???Oct??9 21:07 (218.80.213.242)
service??pts/4? ?? ???Oct??9 21:07 (218.80.213.242)
service??pts/5? ?? ???Oct??9 21:45 (58.31.205.19)
[service@dsg tmp]$ cut -c5 f.txt
i
i
i
i
i
i
[service@dsg tmp]$ cut -c5- f.txt? ?
ice??pts/0? ?? ???Oct??9 20:27 (211.95.114.235)
ice??pts/1? ?? ???Oct??9 21:06 (218.80.203.242)
ice??pts/2? ?? ???Oct??9 14:35 (218.80.203.242)
ice??pts/3? ?? ???Oct??9 21:07 (218.80.213.242)
ice??pts/4? ?? ???Oct??9 21:07 (218.80.213.242)
ice??pts/5? ?? ???Oct??9 21:45 (58.31.205.19)
[service@dsg tmp]$ cut -c1,5,14 f.txt
si0
si1
si2
si3
si4
si5
------------------------------
cut -d -f
-d, --delimiter=DELIM
? ?? ?? ?? ???use DELIM instead of TAB for field delimiter
-f, --fields=LIST
? ?? ?? ?? ???output only these fields;??also print any line that contains??no
? ?? ?? ?? ???delimiter character, unless the -s option is specified
-d -f 主要用來從某中分隔符中提取數據
如:
[service@dsg tmp]$ cat f.txt
service1:pts/0:Oct??9 20:27: (211.95.114.235)
service2:pts/1:Oct??9 21:06: (218.80.203.242)
service3:pts/2:Oct??9 14:35: (218.80.203.242)
service4:pts/3:Oct??9 21:07: (218.80.213.242)
service5:pts/4:Oct??9 21:07: (218.80.213.242)
service6:pts/5:Oct??9 21:45: (58.31.205.19)
[service@dsg tmp]$ cut -d: -f1 f.txt
service1
service2
service3
service4
service5
service6
[service@dsg tmp]$ cut -d: -f2 f.txt
pts/0
pts/1
pts/2
pts/3
pts/4
pts/5
[service@dsg tmp]$ cut -d: -f3 f.txt
Oct??9 20
Oct??9 21
Oct??9 14
Oct??9 21
Oct??9 21
Oct??9 21
[service@dsg tmp]$ cut -d: -f5 f.txt
(211.95.114.235)
(218.80.203.242)
(218.80.203.242)
(218.80.213.242)
(218.80.213.242)
(58.31.205.19)
[service@dsg tmp]$ cut -d: -f9 f.txt
[service@dsg tmp]$ cut -d: -f1,4 f.txt??#提取1和4列數據
service1:27
service2:06
service3:35
service4:07
service5:07
service6:45
Given that the fields are separated by tabs, you should use the -f option to cut instead:
如果文件使用tab鍵隔開就直接使用-f就可以了,-d的默認分隔符號就為tab鍵。
cut -cchars file
如:
? ? -c5? ???提取第5個字符
? ?? ???-c5-? ? 提取第5個字符以后的字符
? ?? ???-c1,5,12??提取多個字符,中間用“,”符號隔開
? ?? ???-c5-14??提取第5個字符到第14個字符間的字符
http://gan.cublog.cn
[service@dsg tmp]$ cat f.txt
service??pts/0? ?? ???Oct??9 20:27 (211.95.114.235)
service??pts/1? ?? ???Oct??9 21:06 (218.80.203.242)
service??pts/2? ?? ???Oct??9 14:35 (218.80.203.242)
service??pts/3? ?? ???Oct??9 21:07 (218.80.213.242)
service??pts/4? ?? ???Oct??9 21:07 (218.80.213.242)
service??pts/5? ?? ???Oct??9 21:45 (58.31.205.19)
[service@dsg tmp]$ cut -c5 f.txt
i
i
i
i
i
i
[service@dsg tmp]$ cut -c5- f.txt? ?
ice??pts/0? ?? ???Oct??9 20:27 (211.95.114.235)
ice??pts/1? ?? ???Oct??9 21:06 (218.80.203.242)
ice??pts/2? ?? ???Oct??9 14:35 (218.80.203.242)
ice??pts/3? ?? ???Oct??9 21:07 (218.80.213.242)
ice??pts/4? ?? ???Oct??9 21:07 (218.80.213.242)
ice??pts/5? ?? ???Oct??9 21:45 (58.31.205.19)
[service@dsg tmp]$ cut -c1,5,14 f.txt
si0
si1
si2
si3
si4
si5
------------------------------
cut -d -f
-d, --delimiter=DELIM
? ?? ?? ?? ???use DELIM instead of TAB for field delimiter
-f, --fields=LIST
? ?? ?? ?? ???output only these fields;??also print any line that contains??no
? ?? ?? ?? ???delimiter character, unless the -s option is specified
-d -f 主要用來從某中分隔符中提取數據
如:
[service@dsg tmp]$ cat f.txt
service1:pts/0:Oct??9 20:27: (211.95.114.235)
service2:pts/1:Oct??9 21:06: (218.80.203.242)
service3:pts/2:Oct??9 14:35: (218.80.203.242)
service4:pts/3:Oct??9 21:07: (218.80.213.242)
service5:pts/4:Oct??9 21:07: (218.80.213.242)
service6:pts/5:Oct??9 21:45: (58.31.205.19)
[service@dsg tmp]$ cut -d: -f1 f.txt
service1
service2
service3
service4
service5
service6
[service@dsg tmp]$ cut -d: -f2 f.txt
pts/0
pts/1
pts/2
pts/3
pts/4
pts/5
[service@dsg tmp]$ cut -d: -f3 f.txt
Oct??9 20
Oct??9 21
Oct??9 14
Oct??9 21
Oct??9 21
Oct??9 21
[service@dsg tmp]$ cut -d: -f5 f.txt
(211.95.114.235)
(218.80.203.242)
(218.80.203.242)
(218.80.213.242)
(218.80.213.242)
(58.31.205.19)
[service@dsg tmp]$ cut -d: -f9 f.txt
[service@dsg tmp]$ cut -d: -f1,4 f.txt??#提取1和4列數據
service1:27
service2:06
service3:35
service4:07
service5:07
service6:45
Given that the fields are separated by tabs, you should use the -f option to cut instead:
如果文件使用tab鍵隔開就直接使用-f就可以了,-d的默認分隔符號就為tab鍵。
轉載于:https://www.cnblogs.com/zhihaowang/archive/2010/07/16/10128662.html
總結
以上是生活随笔為你收集整理的linux cut 命令(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux学习笔记之——Linux硬盘分
- 下一篇: 3COM小型办公室有线局域网方案