码迷,mamicode.com
首页 > 其他好文 > 详细

文本处理的命令

时间:2014-08-29 11:12:08      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:文本处理的命令

一、#wc:print the number of newlines, words, and bytes in files

wc [option] [file]

-l:统计行

-c:统计字节数

-w:统计单词数

如:a、统计当前系统有多少用户: wc -l /etc/passwd

    b、统计/bin下的文件数:ls -l /bin|wc -l

    c、统计/etc目录下以P或者p开头的文件个数:ls -d /etc/[Pp]*|wc -l

二、#tr:translate 转换或删除字符,需要使用管道|


如:a、cat /etc/issue | tr ‘a-z‘ ‘A-Z‘ :将issue中的字母转换为大写


    b、echo "hello world" | tr ‘abcdefg‘ ‘ABCDEFG‘:将hello world 中包含的abcdefg 

    转换为大写。


    c、echo "hello world" | tr -d ‘abcdefg‘:删除hello world 中包含abcdefg中的字符。


    d、head -7 /etc/passwd |tail -1|cut -d: -f1|tr ‘a-z‘ ‘A-Z‘:取出/etc/passwd文件的第7行    中的用户名,并将其转换为大写字符

三、#cut:剪开,根据指定的分隔符,分隔显示。

echo "this is a new line." | cut -d‘ ‘ -f1

cut -d: -f1 /etc/passwd=cat /etc/passwd |cut -d: -f1显示所有用户名

cat /etc/passwd |cut -d: -f1,7显示所有用户以及其shell

head -7 /etc/passwd |tail -1|cut -d: -f1,取出/etc/passwd文件的第7行中的用户名

四、#sort:排序输出文件内容。

sort [option] file

-f:忽略字符大小写。

-n:将内容按照数值比较大小。

-t:指定分隔符

-k:指定分割后进行比较字段

-u:重复行(重复行即连续的行),只显示一次。

如:

a、sort /etc/fstab

b、cat /etc/passwd |cut -d: -f3|sort -n =cut -d: -f3 /etc/passwd| sort -n将所有用户的id排序。

c、cut -d: -f7 /etc/passwd|sort -u 显示当前系统上所有用户的shell,并且杯中shell只显示一次

五、#uniq:移除重复行(重复行即连续的行)

    -c:统计每行重复的次数。

    -d:仅显示重复的行。

    -u:仅显示不重复的行。

六、head和tail:

    取出/etc/passwd文件的第7行:head -7 /etc/passwd |tail -1


文本处理的命令

标签:文本处理的命令

原文地址:http://8757576.blog.51cto.com/8747576/1546415

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!