# cat /proc/meminfo |grep -w "MemTotal"
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設,三水企業(yè)網(wǎng)站建設,三水品牌網(wǎng)站建設,網(wǎng)站定制,三水網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,三水網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
MemTotal:? ? ? 32781228 kB
使用tr命令截取字符串
# cat /proc/meminfo |grep -w "MemTotal" | tr -cd "[0-9]"
32781228
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 命令的’反向引用’章節(jié), \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: 在以上編譯過程中我們發(fā)現(xiàn),編譯器提示說找不到nv_net_tools.h文件,而現(xiàn)實中我們也不需要這個文件的包含了,需要刪除包含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/"http://g')"
echo?from:?$from
to="$(echo?$ret_val?|?sed?'s/.*to\"\:\(.*\)/\1/'?|??cut?-d?','?-f1?|??sed?'s/"http://g')"
echo?to:?$to
url="$(echo?$ret_val?|?sed?'s/.*url\"\:\(.*\)/\1/'?|??cut?-d?','?-f1?|??sed?'s/"http://g')"
echo?url:?$url
md5="$(echo?$ret_val?|?sed?'s/.*md5\"\:\(.*\)/\1/'?|??cut?-d?','?-f1?|?sed?'s/"http://g')"
echo?md5:?$md5
level="$(echo?$ret_val?|?sed?'s/.*level\"\:\(.*\)/\1/'?|??cut?-d?','?-f1?|?sed?'s/"http://g')"
echo?level:?$level12345678910111213141516171819
執(zhí)行結果:
ret_val?is?:?[?"from":"0804020982","to":"0804020998","url":"","md5":"408d261924e6c271200fdf14d3a230cc","level":0?]
from:?0804020982
to:?0804020998
url:?:?408d261924e6c271200fdf14d3a230cc
level:?0
I.總結
要注意命令行執(zhí)行,與腳本文件執(zhí)行之間的差異性。
可以用awk、sed、tr、perl都能實現(xiàn)
set s = '"stratum":{"accepted_shares":1,"algorithm"'
set a = `echo $s | awk -F '["/:]' '{print $4}'`