grep

说明:‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’,直接使用‘egrep’或是‘fgrep’均已不可行了.

参数解释 -E, –extended-regexp 扩展正则表达式egrep -o, –only-matching 只显示一行中匹配PATTERN 的部分</code></pre>

去掉注释及空行

[root@wj conf]# grep -Ev '#|^$' nginx.conf

查找替换

#文本内容替换 参数解释:r 递归调用 l 匹配多个文件时,显示匹配的文件名
grep -rl jdk1.7.0_311 ./ | xargs sed -i 's/jdk1.7.0_311/jdk1.7.0_321/g'
grep -rl 1.94.39.166 ./ | xargs sed -i 's/1.94.39.166/192.168.67.128/g'

查找当前目录某个开头的文件

-v 反匹配 ,不包含
^ 开头
$ 结尾 如1024$ 匹配1024结尾的

[root@localhost ~]# ls
1.txt  2.txt  3.txt  anaconda-ks.cfg  install.sh  OneShellScript.sh  package.xml
[root@localhost ~]# ls | grep -E ^1
1.txt

#多个条件过滤
[root@localhost ~]# ls
1.txt  2.txt  3.txt  anaconda-ks.cfg  install.sh  OneShellScript.sh  package.xml
[root@localhost ~]# ls | grep -E '^1|^2'
1.txt
2.txt

#查找并统计
[root@localhost ~]# ls | grep -E ^1 | wc -l
1

#查找并统计和大小
#xargs 就是把其他命令的给它的数据 传递给它后面的命令作为参数
#-c 除了显示目录和文件大小外,还显示目录或者文件大小的总和
#-h :以 K M G为单位
[root@localhost ~]# ls | grep -E '^1|^2' | xargs du -ch
4.0K	1.txt
4.0K	2.txt
8.0K	总用量


© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容