免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1013 | 回复: 0
打印 上一主题 下一主题

6.15学习日志 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-15 15:15 |只看该作者 |倒序浏览

引自:
http://blog.chinaunix.net/u/19782/showart_1279971.html
一个非常有用的命令,主要用来提取各种各样的数据。
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键。
htp://gan.cublog.cn
更多的cut使用查看man cut
替换字符串
sed -i "s/旧字符串/新字符串/g" `grep 旧字符串 -rl /path`

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/97832/showart_1964846.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP