1、wc命令
wc:word count/calcuate,统计文件中单词情况,大小,行数,一般配合其他命令使用
选项:
-l 统计行数
例如:
[root@linux-87-01 ~]# wc -l /etc/services
11176 /etc/services
[root@linux-87-01 ~]# wc /etc/services
11176 61033 670293 /etc/services
2、查询命令位置
which、shereis
例如:查询ls命令存放的位置
[root@linux-87-01 ~]#
[root@linux-87-01 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@linux-87-01 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@linux-87-01 ~]#
3、文件比较命令
diff、vimdiff
准备两个文件
seq:用于生成数字序列
[root@linux-87-01 ~]# seq 10 > 1.txt
[root@linux-87-01 ~]# seq 10 > 2.txt
[root@linux-87-01 ~]# vim 1.txt
1
2
3
aa
aa
6
7
8
9
10
比较两个文件:
使用diff命令:
[root@linux-87-01 ~]# diff 1.txt 2.txt
4,5c4,5
< aa
< aa
---
> 4
> 5
使用vimdiff:
[root@linux-87-01 ~]# vimdiff 1.txt 2.txt

4、排序组合去重
sort:排序
uniq:去重并统计次数
4.1 sort排序
sort选项:
-n number,把要排序的内容当作是数字,按照数字大小进行排序,默认是升序(小-->大)
-k 指定某一列,根据某一列进行排序
-r reverse,逆序排序
-t 指定分隔符,只能指定1个字符,默认是空格
案例1:sort简单使用
测试文件内容如下:
[root@linux-87-01 ~]# cat sort.txt
4
5
9
91
22
1
10
5
6
11
7
1.11
[root@linux-87-01 ~]#
直接使用sort命令排序:
1
10
11
1.11
22
4
5
5
6
7
9
91
默认是按字母\字符排序,所以要使用-n选项,按数字排序:
[root@linux-87-01 ~]# sort -n sort.txt
1
1.11
4
5
5
6
7
9
10
11
22
91
从大到小排序:
[root@linux-87-01 ~]# sort -nr sort.txt
91
22
11
10
9
7
6
5
5
4
1.11
1
案例2:按指定某一列排序
[root@linux-87-01 ~]# cat test.txt
1 a 15
4 b 82
7 w 14
5 c 10
按第3列按数字逆序排序:
[root@linux-87-01 ~]# sort -rn -k3 test.txt
4 b 82
1 a 15
7 w 14
5 c 10
案例3:ps命令是用于查看Linux进程信息的命令。ps aux用于查看进程较为详细的信息
需求:对ps aux结果中内存的列进行排序,取出前5个。
# 1. ps aux
第3列是cpu使用情况,第4列是内存使用情况
# 2. 使用管道(|),将ps aux的结果传给sort,然后再使用管道传给head命令
[root@linux-87-01 ~]# ps aux | sort -rn -k 4 | head -5

案例4:指定分隔符进行排序
需求:对passwd文件的第3列进行逆序排序
[root@linux-87-01 ~]# cp /etc/passwd .
[root@linux-87-01 ~]# sort -t ':' -rnk3 passwd
4.2 uniq去重
uniq:去重,去重的前提是,已经排好序
选项:-c 去重并显示重复的次数
准备1.txt文件如下:
[root@linux-87-01 ~]# cat > 1.txt <<EOF
> a
> a
> b
> b
> 1
> 1
> 2
> 2
> 2
> 3
> EOF
uniq命令使用:
[root@linux-87-01 ~]# uniq -c 1.txt
2 a
2 b
2 1
3 2
1 3
[root@linux-87-01 ~]#
4.3 实战案例-日志分析
/var/log/secure日志,记录用户登录的情况
通过分析secure文件中的ip部分(secure.txt),分析每个ip出现的次数,取出前20个。
secure.txt只含有ip信息。
sort secure.txt | uniq -c | sort -rnk1 | head -20