Linux中使用grep命令,查询多个字符串

Linux中使用grep命令,查询多个字符串,第1张

grep 搜索同时满足多个关键字和满足任意关键字

1、 grep -E "word1|word2|word3" file.txt

满足任意条件(word1、word2和word3之一)将匹配。

这样也可以

egrep 'word1|word2|word3' 文件名

grep 'word1/|word2|word3' 文件名

错误的:

grep 'word1|word2|word3'

2、 grep word1 file.txt | grep word2 |grep word3

必须同时满足三个条件(word1、word2和word3)才匹配。

为什么需要加-E

1. 例子

提取字符串以下字符串 error: 与 : 之间的子字符串。并消除空格。

"src/network/misc/nv_net_udp.c:17: fatal error: nv_net_tools.h: No such file or directory"1

2. 命令

echo "src/network/misc/nv_net_udp.c:17: fatal error: nv_net_tools.h: No such file or directory" | sed -r 's/.*error\:(.*)\:.*/\1/' | sed s/[[:space:]]//g1

该命令涉及 sed 命令的’反向引用’章节, \1 代表 (.*)

3. 结果

nv_net_tools.h1

4. 拓展 : 解决编译错误

leon$  make clean && make all arm-hisiv100nptl-linux-gcc -c -fPIC -Wall -g -ggdb -O0 -DHI3518_CHIP -DLINUX -I./src/include -I./src/include/common -I./src/include/camera -I./src/include/conf -I./src/include/log -I./src/include/network -I./src/include/upnp -I/home/leon/nvc/arm-hisiv100nptl-linux/include src/common/nv_conf.c -o build/objs/hi3518/src/common/nv_conf.o/*省略中间部分*/arm-hisiv100nptl-linux-gcc -c -fPIC -Wall -g -ggdb -O0 -DHI3518_CHIP -DLINUX -I./src/include -I./src/include/common -I./src/include/camera -I./src/include/conf -I./src/include/log -I./src/include/network -I./src/include/upnp -I/home/leon/nvc/arm-hisiv100nptl-linux/include src/network/misc/nv_net_udp.c -o build/objs/hi3518/src/network/misc/nv_net_udp.o

src/network/misc/nv_net_udp.c:17: fatal error: nv_net_tools.h: No such file or directory

compilation terminated.make: *** [build/objs/hi3518/src/network/misc/nv_net_udp.o] Error 112345678

note: 在以上编译过程中我们发现,编译器提示说找不到nv_net_tools.h文件,而现实中我们也不需要这个文件的包含了,需要删除包含nv_net_tools.h 文件中的该行代码。

5.字符串提取 (命令行)

目标字符串

"from":"0802070975","to":"0802071013","url":"http://cn.update.mynetvue.com:8060/firmware/ce7e4228521be4556f69ef2bfcb7d665.tar.gz","md5":"ace4b34383f964442a591559d0afb296","level":0 12

需要提取 from , to, url , md5, level 等5个关键字后面的内容

//提取 from  内容echo

"from":"0802070975","to":"0802071013","url":"http://cn.update.mynetvue.com:8060/firmware/ce7e4228521be4556f69ef2bfcb7d665.tar.gz","md5":"ace4b34383f964442a591559d0afb296","level":0 |  sed "s/.*from:\(.*\)/\1/" |  cut -d ',' -f1

//提取 to 内容echo

"from":"0802070975","to":"0802071013","url":"http://cn.update.mynetvue.com:8060/firmware/ce7e4228521be4556f69ef2bfcb7d665.tar.gz","md5":"ace4b34383f964442a591559d0afb296","level":0 |  sed "s/.*to:\(.*\)/\1/" |  cut -d ',' -f1

//提取 url  内容echo

"from":"0802070975","to":"0802071013","url":"http://cn.update.mynetvue.com:8060/firmware/ce7e4228521be4556f69ef2bfcb7d665.tar.gz","md5":"ace4b34383f964442a591559d0afb296","level":0 |  sed "s/.*url:\(.*\)/\1/" |  cut -d ',' -f1

//提取 md5 内容echo

"from":"0802070975","to":"0802071013","url":"http://cn.update.mynetvue.com:8060/firmware/ce7e4228521be4556f69ef2bfcb7d665.tar.gz","md5":"ace4b34383f964442a591559d0afb296","level":0 |  sed "s/.*md5:\(.*\)/\1/" |  cut -d ',' -f1

//提取 level 内容echo

"from":"0802070975","to":"0802071013","url":"http://cn.update.mynetvue.com:8060/firmware/ce7e4228521be4556f69ef2bfcb7d665.tar.gz","md5":"ace4b34383f964442a591559d0afb296","level":0 |  sed "s/.*level:\(.*\)/\1/" |  cut -d ',' -f112345678910111213141516

6.字符串提取 (脚本文件)

ret_val="\"from\":\"0804020982\",\"to\":\"0804020998\",\"url\":\"http://cn.update.mynetvue.com:8060/firmware/ce7e4228521be4556f69ef2bfcb7d665.tar.gz\",\"md5\":\"408d261924e6c271200fdf14d3a230cc\",\"level\":0" echo ret_val is : [ $ret_val ]from="$(echo $ret_val | sed 's/.*from\"\:\(.*\)/\1/' |  cut -d ',' -f1 |  sed 's/"//g')"

echo from: $from to="$(echo $ret_val | sed 's/.*to\"\:\(.*\)/\1/' |  cut -d ',' -f1 |  sed 's/"//g')"

echo to: $to url="$(echo $ret_val | sed 's/.*url\"\:\(.*\)/\1/' |  cut -d ',' -f1 |  sed 's/"//g')"

echo url: $url md5="$(echo $ret_val | sed 's/.*md5\"\:\(.*\)/\1/' |  cut -d ',' -f1 | sed 's/"//g')"

echo md5: $md5 level="$(echo $ret_val | sed 's/.*level\"\:\(.*\)/\1/' |  cut -d ',' -f1 | sed 's/"//g')"

echo level: $level12345678910111213141516171819

执行结果:

ret_val is : [ "from":"0804020982","to":"0804020998","url":"http://cn.update.mynetvue.com:8060/firmware/ce7e4228521be4556f69ef2bfcb7d665.tar.gz","md5":"408d261924e6c271200fdf14d3a230cc","level":0 ]

from: 0804020982

to: 0804020998

url: http://cn.update.mynetvue.com:8060/firmware/ce7e4228521be4556f69ef2bfcb7d665.tar.gzmd5: 408d261924e6c271200fdf14d3a230cc

level: 0

I.总结

要注意命令行执行,与脚本文件执行之间的差异性。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8922903.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-23
下一篇 2023-04-23

发表评论

登录后才能评论

评论列表(0条)

保存