grep命令常用参数
–color 显示颜色
-n 显示行号
-o 只输出匹配的字符,用于统计字符在文中出现的次数
-c 计算搜索到的字符串的次数
-v 输出与匹配相反的字符
-i 不区分大小写
-w 按单词搜索 相当于\b单词\b 单词边界 精确匹配
-E 使用扩展正则表达式
-A 除了显示匹配的一行外,并显示该行之后的n行
-B 除了显示匹配的一行外,并显示该行之前的n行
-C 除了显示匹配的一行外,并显示该行前后的n行
-q 安静模式,不打印任何标准输出。如果有匹配的内容则立即返回状态值0
grep -o “\w” 每个字符单独一行
例:
1打印配置文件/etc/passwd 内容的行号以及内容
grep -n ‘.’ /etc/passwd
grep -n $ /etc/passwd
2 提取文件中指定单词出现的次数
The months of learning in Old Boy education are the few months that I think the time efficient is the most.I had also studied at other training institutions before, but I was hard to understand what the tutor said and hard to follow. It was just too much to learn with no outline.
[root@tom01 ~]# grep -iow “\bthe\b” test.txt |uniq -c
1 The
4 the
3 去除文件空白行
grep -v “^$” README.txt
grep “.” README.txt
4 只查看文件第10行到20行内容
grep 10 -A 10 /etc/passwd
5 提取ip地址
grep “IPADDR” /etc/sysconfig/network-scripts/ifcfg-eth0|cut -d= -f2
192.168.56.7
6 统计服务器已连接IP
netstat -ant |grep “ES”|awk ‘{print $4}’|cut -d: -f1