共计 879 个字符,预计需要花费 3 分钟才能阅读完成。
目的
熟练使用 cut、sort、uniq、wc 等命令应用。cut 命令功能:从文件的每一行截取一段内容;sort 命令功能:把文本文件的行排序;uniq 命令功能:报告或忽略重复的行;wc 命令功能:为文件打印行数、单词数、字节数。
前提
可用的 centos7 系统,连接网络。
命令介绍
1、cut 命令:按列抽取文本内容
【例 1】截取 /etc/passwd 文件第一行,以冒号为分隔符,抽取第 7 个字段
[root@Magedu ~]# head -1 /etc/passwd | |
root:x:0:0:root:/root:/bin/bash | |
[root@Magedu ~]# head -1 /etc/passwd | cut -d: -f7 | |
/bin/bash |
2、sort 命令:文本排序
【例 2】以 1.sh 文件一行内容的空格分隔,按第 3 段从大到小排序
[root@Magedu ~]# cat 1.sh | |
this is 111 line | |
this is 222 line | |
this is 333 line | |
this is 444 line | |
this is 555 line | |
this is 666 line | |
this is 777 line | |
this is 888 line | |
this is 999 line | |
[root@Magedu ~]# cat 1.sh |sort -k3 -r | |
this is 999 line | |
this is 888 line | |
this is 777 line | |
this is 666 line | |
this is 555 line | |
this is 444 line | |
this is 333 line | |
this is 222 line | |
this is 111 line |
3、wc 命令:文本数据统计
【例 3】统计 /etc/pass 文件有多少行
[ | ]|
50 |
4、uniq 命令:文本去重
【例 4】统计 2.sh 文件中相同内容的行出现的次数
[root@Magedu ~]# cat 2.sh | |
this is 111 line | |
this is 111 line | |
this is 111 line | |
this is 111 line | |
this is 111 line | |
[root@Magedu ~]# uniq -c 2.sh | |
5 this is 111 line |
文章来源于网络,侵删!
正文完
星哥玩云-微信公众号
