1. 例子
提取字符串以下字符串 error: 与 : 之间的子字符串。并消除空格。
"src/network/misc/nv_net_udp.c:17: fatal error: nv_net_tools.h: No such file or directory"12. 命令
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.h14. 拓展 : 解决编译错误
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.osrc/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 ',' -f1123456789101112131415166.字符串提取 (脚本文件)
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.总结
要注意命令行执行,与脚本文件执行之间的差异性。
grep ingcn -rl /assign-path | xargs sed -i 's/ingcn/.ingcn/g'或
sed -i 's/ingcn/.ingcn/g' `grep ingcn -rl /assign-path`
还有一种比较复杂的写法:
grep ingcn * -R | awk -F: '{print $1}' | sort | uniq | xargs sed -i 's/ingcn/.ingcn/g'
其中命令awk用于分割字符串、uniq对结果进行除重。
、网络上现成的资料
格式: sed -i s/查找字段/替换字段/g `grep 查找字段 -rl 路径`
linux sed 批量替换多个文件中的字符串
sed -i s/oldstring/newstring/g `grep oldstring -rl yourdir`
例如:替换/home下所有文件中的.111cn.为111cn.
sed -i s/.111cn./111cn./g `grep .111cn. -rl /home`
exp:sed -i s/shabi/$/g `grep shabi -rl ./`
2、自己额外附加
2.1 将文件1.txt内的文字garden替换成mirGarden
# sed -i s/garden/mirGarden/g
1.txt sed -i 很简单
2.2 将当前目录下的所有文件内的garden替换成mirGarden
## sed -i s/garden/mirGarden/g `ls` 其实也就是ls出多个文件名而已
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)