<span style="background-color: rgb(255, 102, 102);">
</span>
grep 支持正则表达式
grep 以整行为单位 grep “yang” /home/yhh
-------------------------------------------------
-n 显示行数
-v 方向搜索
-i 不计大小写
-A3 after 显示前三行
-B3 before 显示后三行
------------------------------------------------------
grep -n -A3 -B3 --color=auto 'eth' yhh.txt
find . -type f |xargs grep -A3 -b3 -i 'Yang‘ 查找当前目录下所有文件中含有Yang(不区分大小)的文件,并输出前后三行
xargs 是给命令传递参数的一个个油滤器,他将一个大的数据流分成一个个小的数据流,以方便命令处理
grep -vn 'eth' yhh.txt 查找不带eth的行
grep -in 'eth' yhh.txt 不计大小写的查找eth的行
-------------------------------------------------------—-----------------------------------------------------------
[] 集合字符
grep -n 't[ae]st' yhh.txt 在文件中查找tast或test的行
grep -n '[^g]oo' yhh.txt 在文件中查找oo前不带g的行
grep -n '[^a-z]oo' yhh.txt 在文件中查找oo前不带小写字母的行
----------------------------------------------------------------------------------------------------
^ $ 行首 行尾
grep -n '^[a-z]' yhh.txt 在文件中查找以小写字母开头的行
grep -n '^[[:lower:]]' yhh.txt
greo -n '^[^a-zA-Z]' yhh.txt
grep -n '\.$' yhh.txt 以.结尾的行
-------------------------------------------------------------------------------------
. 任意一个字符 * 重复前一个字符0-到任意多次
grep 'g..d' yhh.txt 查找gxxd的行
grep ‘o*’ yhh.txt 输出全部,因为可以为0个字符匹配
grep ‘oo*’ yhh.txt 查找含有O的行
grep ‘g.*d’ yhh.txt 查找以g开头d结尾的字符串的行
----------------------------------------------------------------------------------------
\{\} 限定重复字符范围
grep 'go\{2,5\}' yhh.txt 查找goo gooo goooo goooo
grep 'go\{2\}' yhh.txt goo
grep 'go\{2,\}' yhh.txt goo以上
------------------------------------------------------------------------------------
查找/home/yhh下以test开头的文件
[root@localhost yhh]# find /home/yhh/test*
/home/yhh/test.t
/home/yhh/test.tar.gz
查找/home/yhh及其子目录下以test开头的文件,包括目录
[root@localhost yhh]# find /home/yhh -name 'test*'
/home/yhh/.cache/mozilla/firefox/gqk7nty1.default/safebrowsing/test-malware-simple.sbstore
/home/yhh/.cache/mozilla/firefox/gqk7nty1.default/safebrowsing/test-malware-simple.cache
/home/yhh/.cache/mozilla/firefox/gqk7nty1.default/safebrowsing/test-malware-simple.pset
/home/yhh/.cache/mozilla/firefox/gqk7nty1.default/safebrowsing/test-phish-simple.sbstore
/home/yhh/.cache/mozilla/firefox/gqk7nty1.default/safebrowsing/test-phish-simple.cache
/home/yhh/.cache/mozilla/firefox/gqk7nty1.default/safebrowsing/test-phish-simple.pset
/home/yhh/test.tar.gz
/home/yhh/test.t
find /home/yhh -type f -name "*" |查找yhh目录及其子目录下的所有文件,包括目录
[root@localhost yhh]# find /home/yhh -name "*" -type f|xargs grep "yang">cp1
[root@localhost yhh]# ls *~
test~
[root@localhost yhh]# ls *~|rm -rf
[root@localhost yhh]# ls
cp1 Documents maillog Public test.t
cptest.txt Downloads Music Templates test.tar.gz
Desktop dsf Pictures test~ Videos
[root@localhost yhh]# ls *~|xargs rm -rf
[root@localhost yhh]# ls
cp1 Desktop Downloads maillog Pictures Templates test.tar.gz
cptest.txt Documents dsf Music Public test.t Videos
[root@localhost yhh]#
grep -vn 'the' regular_express.txgrep -vn 'the' regular_express.txt
t
grep -vn 'the' regular_express.txt
grep -vn 'thgrep -vn 'the' regular_express.txt
e' regular_express.txt