Linux 系统中,有很多用于快速处理数据的工具如grep awk cut sort uniq sort,他们非常非常地好用。 如果你熟练掌握他们的使用技巧,他们则可以帮你快速定位问题。
一、sort
Usage: sort [OPTION]... [FILE]...
-o 输出文件
-d 按字典顺序排序
-n 按数据大小输出
-r 按逆序输出排序结果
-k 指定分类是域上的数字分类
-t 域分隔符,用非空格或tab分隔域
sort -k3 -n -r -t: /etc/passwd
sort -d /etc/passwd
二、WC
Usage: wc [OPTION]... [FILE]...
-c 字符数量~
-l 行数~
-w 统计单词数量~
wc /etc/passwd
36
三、diff
diff
Usage: diff [OPTION]... FILES
Compare files line by line.
-q 显示有无差异,不显示详细的信息~
-c 显示全部内文,并标出不同之处~
-b 不检查空格字符的不同~
-B 不检查空白行
-r 比较子目录中的文件~
diff /etc/passwd ./passwd.bak
2d1
< bin:x:1:1:bin:/bin:/sbin/nologin
四、grep
Usage: grep [OPTION]... PATTERN [FILE] ...
Search for PATTERN in each FILE or standard input.
-c 只打印匹配的行编号数
-i 匹配文本时忽略大小写
-n 在每行前显示其行编号
-v 逆向输出,打印不匹配的行
-f file 要匹配的字符在文件列表中
cat /etc/passwd |grep -n root
1:root:x:0:0:root:/root:/bin/bash
12:operator:x:11:0:operator:/root:/sbin/nologin
#grep '[Tt]his' file.txt
#grep '^[^#]' file.txt
匹配任意字符
grep 'r..t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
五、sed
sed
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
S 替代操作
i 插入命令
a 附加命令
d 删除全部匹配行
D 删除首次匹配的行
#sed -n '1,4p' /etc/passwd 打印1~4行,-n --quiet以免先打印出passwd的全部内容
#sed '/80/D' file.txt
#sed 's/var/usr/g' file.txt 替换file.txt中全部var为usr
#sed '50,$s/help/man/g' file.txt 从50~最后行替换help为man
sed '/done/d' xj_user_p.log 删除done
六、awk
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
NF 当前记录中的字段数。NR 当前记录数。
awk -F: '{print NR,$1,$NF}' ./passwd.bak
awk -F: 'NR==5{print NR,$0}' ./passwd.bak 打印出5,15,25...行
5 sync:x:5:0:sync:/sbin:/bin/sync
15 nobody:x:99:99:Nobody:/:/sbin/nologin
25 apache:x:48:48:Apache:/var/www:/sbin/nologin
七、uniq
如果要在文件中查找重复的行,uniq命令会很有用,该命令一般格式为:uniq in_file out_file
$ more test.txt
aaa
ccc
ccc
ccc
ddd
bbb
eee
123
$ uniq test.txt
aaa
ccc
ddd
bbb
eee
123
$ uniq -d test.txt
ccc
$ uniq -c test.txt
$
综合示例:
...
数据处理:
找出上海股票涨幅最大的股票?
sort -n -r -k4 t.txt | sed -n '1p'
涨幅>3的股票?
awk '{if ($1>3) print $0} ' t.txt
涨幅在在4~15之间的股票
awk '{if($4>0&&$4<15){print $0}}' t.txt
最近碰到一个案例。 一个项目上数据库系统, ORACLE ,运行在 LINUX 上,报进程数满了。看到这类问题,您不需要去深究什么警告日志,数据库中进程状况啊等等。马上 在操作系统层面上看看有哪些服务器连接到这台数据库服务器上。
(墙内链接: http://mikixiyou.iteye.com/blog/1538828)
这里使用的第一个工具就netstat 。他是系统管理工具,还不能算shell 工具。
通过netstat -ntu ,找出通过tcp 和udp 连接服务器的IP 地址列表。
[root@webdb4 ~]# netstat -ntu|more
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.15.209:1521 192.168.15.65:37781 ESTABLISHED
tcp 0 0 192.168.15.209:1521 192.168.15.65:37783 ESTABLISHED
tcp 0 0 192.168.15.209:1521 192.168.15.65:37777 ESTABLISHED
tcp 0 0 192.168.15.209:1521 192.168.15.65:37779 ESTABLISHED
tcp 0 0 10.100.15.209:49895 10.100.15.207:26069 ESTABLISHED
tcp 0 0 10.100.15.209:49898 10.100.15.213:6092 ESTABLISHED
tcp 0 0 192.168.15.209:1521 192.168.15.65:37785 ESTABLISHED
tcp 0 0 192.168.15.209:1521 192.168.15.181:21869 ESTABLISHED
tcp 0 0 192.168.15.209:1521 192.168.15.181:21870 ESTABLISHED
tcp 1 0 192.168.15.209:26781 192.168.15.89:1521 CLOSE_WAIT
tcp 0 0 192.168.15.209:10332 192.168.15.211:6200 ESTABLISHED
tcp 0 0 192.168.15.209:1521 192.168.15.181:21875 ESTABLISHED
tcp 0 0 192.168.15.209:1521 192.168.15.65:37766 ESTABLISHED
tcp 0 0 192.168.15.209:1521 192.168.15.181:21874 ESTABLISHED
tcp 0 0 192.168.15.209:1521 192.168.15.65:37761 ESTABLISHED
下面略掉,太多了,没法显示全。
这是所有与数据库服务器连接的外部IP 信息列表。
可以看到第五列是所有的外部IP 信息。根据这些信息,找出数目那个IP 是什么?或者根据IP数目做一个排序。
第一步,使用grep 将tcp 过滤出来,也可以使用egrep 过滤多个条件
netstat -ntu|grep 'tcp'
netstat -ntu|egrep 'tcp|udp'
第二步,使用awk 将第五列单独截出来
netstat -ntu|grep 'tcp'|awk '{print $5}'
操作如下:
[root@webdb4 ~]# netstat -ntu|grep 'tcp'|awk '{print $5}'|more
192.168.15.65:37781
192.168.15.61:34281
192.168.15.65:37783
192.168.15.61:34282
192.168.15.65:37777
192.168.15.65:37779
10.100.15.207:26069
192.168.15.61:34275
10.100.15.213:6092
第三步,使用cut 将列信息以":" 为分隔符再分成不同的列, 显示第一个field
netstat -ntu|grep 'tcp'|awk '{print $5}'|cut -d: -f1
第四步,使用sort 默认字符顺序将字段值排序
netstat -ntu|grep 'tcp'|awk '{print $5}'|cut -d: -f1|sort
sort 有很多选项,可以man sort 去看。
第五步,使用uniq 将已经排序好的字段计算不同值的数目
netstat -ntu|grep 'tcp'|awk '{print $5}'|cut -d: -f1|sort|uniq -c
uniq -c 根据相近的值计算和,因此之前需要排序好。
第六步,使用sort -n 数字升序方式再排一下结果
netstat -ntu|grep 'tcp'|awk '{print $5}'|cut -d: -f1|sort|uniq -c|sort -n
使用sort -nr ,可以按照数字降序排。
netstat -ntu|grep 'tcp'|awk '{print $5}'|cut -d: -f1|sort|uniq -c|sort -nr
第七步,使用head 或tail 取头部几行或尾部几行
取尾部10 行
netstat -ntu|grep 'tcp'|awk '{print $5}'|cut -d: -f1|sort|uniq -c|sort -n|tail -10
取头部10 行
netstat -ntu|grep 'tcp'|awk '{print $5}'|cut -d: -f1|sort|uniq -c|sort -nr|head -10
[root@webdb4 ~]# netstat -ntu|grep 'tcp'|awk '{print $5}'|cut -d: -f1|sort|uniq -c|sort -n|tail -10
8 192.168.15.133
8 192.168.15.62
10 192.168.15.181
11 192.168.15.61
12 192.168.15.204
15 192.168.15.63
17 192.168.15.100
18 192.168.15.92
30 192.168.15.65
32 192.168.15.146
[root@webdb4 ~]# netstat -ntu|grep 'tcp'|awk '{print $5}'|cut -d: -f1|sort|uniq -c|sort -nr|head -10
32 192.168.15.146
30 192.168.15.65
18 192.168.15.92
17 192.168.15.100
15 192.168.15.63
12 192.168.15.204
11 192.168.15.61
10 192.168.15.181
8 192.168.15.62
8 192.168.15.133
这就得到我们要的结果。
这个结果中,我们可以看出各个服务器在数据库服务器上的网络连接数。
根据连接数,发现特别大的,肯定有问题。通常都应用服务端打开连接不关闭,或者出现异常无法关闭。