Linux常用命令收集
1、统计文档中字符出现的次数-思路:grep出完全匹配字符的行数,且仅输出匹配的字符而不是匹配行,再wc统计行数
bash-4.1# cat /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
nobody:x:99:99:Nobody:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
bash-4.1# grep -ow 'halt' /etc/passwd |wc -l
3
bash-4.1# grep -ow 'halt' /etc/passwd
halt
halt
halt
bash-4.1# grep 'halt' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
bash-4.1#
bash-4.1# echo '012233' |grep -o 2|wc -l
2
bash-4.1#
bash-4.1#
bash-4.1# echo '012233' |grep -o 22|wc -l
1
bash-4.1#
2、文件里出现的字符次数统计和排序(去空白行)
awk -F ':|/' '{ for ( i = 1; i <= NF; i++ ) print $i }' /etc/passwd |sed '/^$/d'|sort |uniq -c|sort -nr > 1.txt
3、只打印文件的奇数行
[root@localhost linshi]# seq 1 10 > c.txt && seq 1 10 >> c.txt
[root@localhost ~]# awk '{a[NR]=$0}END{for(i=1;i<=NR;i+=2){print i,a[i]}}' c.txt
1 1
3 3
5 5
7 7
9 9
11 1
13 3
15 5
17 7
19 9
[root@localhost ~]# awk '{a[NR]=$0}END{for(i in a){print i,a[i]}}' c.txt | wc -l
20
[root@localhost linshi]# awk '{a[NR]=$0}END{for(i in a){print i,a[i]}}' c.txt
17 7
4 4
18 8
5 5
19 9
6 6
7 7