Linux命令怎么截取两个字符之间的字符串? 如1111[2222]3333 怎么截取2222?前

Linux命令怎么截取两个字符之间的字符串? 如1111[2222]3333 怎么截取2222?前,第1张

这个很简单啦,写一个给你吧,最简单又能理解的

echo '1111[2222]3333' | awk -F '[' '{ print $2 }' | awk -F ']' '{ print $1 }'

或者

echo '1111[2222]3333' | cut -d '[' -f 2 | cut -d ']' -f 1

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/8999894.html

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

发表评论

登录后才能评论

评论列表(0条)

保存