linux正则表达式语法

来源:互联网 发布:vscode怎么编译运行 编辑:程序博客网 时间:2024/06/05 18:17

grep查找字符时以整行为单位

查找包含eth的内容

 dmesg |grep 'eth'

[ 1.857820] e1000 0000:02:01.0 eth0: (PCI:66MHz:32-bit) 00:0c:29:f4:75:d2
[ 1.857824] e1000 0000:02:01.0 eth0: Intel(R) PRO/1000 Network Connection

显示行号,并将捕捉到的单词加上行号

dmesg |grep -n --color=auto 'eth'

在关键词所在行的前三后四行也一起显示

dmesg |grep -n A3 -B4 --color=auto 'eth'

在指定文件中搜索特定字符串

grep -n 'the' regular-express.txt

搜索不包含指定字符串的内容

grep -vn 'the' regular-expres.txt

不区分大小写查找

grep -in 'the' regular_express.txt

查找含有tast或test的字符

grep -n 't[ae]st' regular_express.txt

反向选择[^],查找不包含g字符,但含有oo的内容

grep -n ‘[^g]oo’ regular_express.txt

0 0