linux命令截取字符串,linux 字符串截取命令
linux字符串提取数字
# cat /proc/meminfo |grep -w "MemTotal"
成都创新互联是一家集网站建设,三水企业网站建设,三水品牌网站建设,网站定制,三水网站建设报价,网络营销,网络优化,三水网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
MemTotal: 32781228 kB
使用tr命令截取字符串
# cat /proc/meminfo |grep -w "MemTotal" | tr -cd "[0-9]"
32781228
linux有什么命令可以提取指定的字符串
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":"","md5":"ace4b34383f964442a591559d0afb296","level":0 12
需要提取 from , to, url , md5, level 等5个关键字后面的内容。
//提取 from 内容echo
"from":"0802070975","to":"0802071013","url":"","md5":"ace4b34383f964442a591559d0afb296","level":0 | sed "s/.*from:\(.*\)/\1/" | cut -d ',' -f1
//提取 to 内容echo
"from":"0802070975","to":"0802071013","url":"","md5":"ace4b34383f964442a591559d0afb296","level":0 | sed "s/.*to:\(.*\)/\1/" | cut -d ',' -f1
//提取 url 内容echo
"from":"0802070975","to":"0802071013","url":"","md5":"ace4b34383f964442a591559d0afb296","level":0 | sed "s/.*url:\(.*\)/\1/" | cut -d ',' -f1
//提取 md5 内容echo
"from":"0802070975","to":"0802071013","url":"","md5":"ace4b34383f964442a591559d0afb296","level":0 | sed "s/.*md5:\(.*\)/\1/" | cut -d ',' -f1
//提取 level 内容echo
"from":"0802070975","to":"0802071013","url":"","md5":"ace4b34383f964442a591559d0afb296","level":0 | sed "s/.*level:\(.*\)/\1/" | cut -d ',' -f112345678910111213141516
6.字符串提取 (脚本文件)
ret_val="\"from\":\"0804020982\",\"to\":\"0804020998\",\"url\":\"\",\"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":"","md5":"408d261924e6c271200fdf14d3a230cc","level":0 ]
from: 0804020982
to: 0804020998
url: : 408d261924e6c271200fdf14d3a230cc
level: 0
I.总结
要注意命令行执行,与脚本文件执行之间的差异性。
linux 中如何截取2个字符串中间的字符?
可以用awk、sed、tr、perl都能实现
set s = '"stratum":{"accepted_shares":1,"algorithm"'
set a = `echo $s | awk -F '["/:]' '{print $4}'`
当前文章:linux命令截取字符串,linux 字符串截取命令
URL地址:http://scyanting.com/article/hdhpjo.html