sort命令和wc命令分析

不点 阅读:208 2021-07-29 11:57:06 评论:0

sort 命令

功能:对文件进行排序
格式:sort [选项] 文件名
选项:

-f:忽略大小写
-n:以数值型进行排序,默认使用字符串型排序
-r:反向排序
-t:指定分隔符,默认分隔符是制表符
-k n[,m]:按照指定的字段范围排序。从第n字段开始,m字段结束(默认到行尾)

示例

按照第一列字母a-z进行排序

[root@localhost home]# sort /etc/passwd 
adm:x:3:4:adm:/var/adm:/sbin/nologin 
apache:x:48:48:Apache:/var/www:/sbin/nologin 
bin:x:1:1:bin:/bin:/sbin/nologin 
cls:x:500:889:dgdzmx:/home/cls:/bin/bash 
daemon:x:2:2:daemon:/sbin:/sbin/nologin 
dbus:x:81:81:System message bus:/:/sbin/nologin 
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 
games:x:12:100:games:/usr/games:/sbin/nologin 
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin 
halt:x:7:0:halt:/sbin:/sbin/halt 
imooc:x:502:889:kk,kk,kk,kk:/home/xxx:/bin/bash 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
lw:x:505:505::/home/lw:/bin/bash 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 
nginx:x:498:498:nginx user:/var/cache/nginx:/sbin/nologin 
nobody:x:99:99:Nobody:/:/sbin/nologin 
ntp:x:38:38::/etc/ntp:/sbin/nologin 
operator:x:11:0:operator:/root:/sbin/nologin 
postfix:x:89:89::/var/spool/postfix:/sbin/nologin 
root:x:0:0:root:/root:/bin/bash 
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 
sync:x:5:0:sync:/sbin:/bin/sync 
tony:x:504:504::/home/tony:/bin/bash 
user1:x:503:503::/home/user1:/bin/bash 
user2:x:506:506::/home/user2:/bin/bash 
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin 
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin 
[root@localhost home]#  

使用“:”分隔符分割,并按照id进行排序

[root@localhost home]# sort -t ":" -k "3,3" /etc/passwd 
root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin 
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin 
operator:x:11:0:operator:/root:/sbin/nologin 
games:x:12:100:games:/usr/games:/sbin/nologin 
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin 
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 
daemon:x:2:2:daemon:/sbin:/sbin/nologin 
adm:x:3:4:adm:/var/adm:/sbin/nologin 
ntp:x:38:38::/etc/ntp:/sbin/nologin 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
apache:x:48:48:Apache:/var/www:/sbin/nologin 
nginx:x:498:498:nginx user:/var/cache/nginx:/sbin/nologin 
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin 
sync:x:5:0:sync:/sbin:/bin/sync 
cls:x:500:889:dgdzmx:/home/cls:/bin/bash 
imooc:x:502:889:kk,kk,kk,kk:/home/xxx:/bin/bash 
user1:x:503:503::/home/user1:/bin/bash 
tony:x:504:504::/home/tony:/bin/bash 
lw:x:505:505::/home/lw:/bin/bash 
user2:x:506:506::/home/user2:/bin/bash 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin 
halt:x:7:0:halt:/sbin:/sbin/halt 
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 
dbus:x:81:81:System message bus:/:/sbin/nologin 
postfix:x:89:89::/var/spool/postfix:/sbin/nologin 
nobody:x:99:99:Nobody:/:/sbin/nologin 
[root@localhost home]#  

注:因为当成字符串排序了

[root@localhost home]# sort -n -t ":" -k "3,3" /etc/passwd 
root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin 
daemon:x:2:2:daemon:/sbin:/sbin/nologin 
adm:x:3:4:adm:/var/adm:/sbin/nologin 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
sync:x:5:0:sync:/sbin:/bin/sync 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 
halt:x:7:0:halt:/sbin:/sbin/halt 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin 
operator:x:11:0:operator:/root:/sbin/nologin 
games:x:12:100:games:/usr/games:/sbin/nologin 
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin 
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 
ntp:x:38:38::/etc/ntp:/sbin/nologin 
apache:x:48:48:Apache:/var/www:/sbin/nologin 
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin 
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 
dbus:x:81:81:System message bus:/:/sbin/nologin 
postfix:x:89:89::/var/spool/postfix:/sbin/nologin 
nobody:x:99:99:Nobody:/:/sbin/nologin 
nginx:x:498:498:nginx user:/var/cache/nginx:/sbin/nologin 
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin 
cls:x:500:889:dgdzmx:/home/cls:/bin/bash 
imooc:x:502:889:kk,kk,kk,kk:/home/xxx:/bin/bash 
user1:x:503:503::/home/user1:/bin/bash 
tony:x:504:504::/home/tony:/bin/bash 
lw:x:505:505::/home/lw:/bin/bash 
user2:x:506:506::/home/user2:/bin/bash 
[root@localhost home]#  

注意:-n按照数值进行排序

wc命令
功能:统计
格式:wc [选项] 文件名
选项:

-l:只统计行数
-w:只统计单词数
-m:只统计字符串

示例

[root@localhost home]# wc /etc/passwd 
  29   38 1278 /etc/passwd 
[root@localhost home]# wc -l /etc/passwd 
29 /etc/passwd 
[root@localhost home]# wc -w /etc/passwd 
38 /etc/passwd 
[root@localhost home]# wc -m /etc/passwd 
1278 /etc/passwd 
[root@localhost home]# 

标签:linux
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号