sed匹配JSON数据中的数字



 {"list":[{"fs_id":4176732013,"path":"\/apps\/zjhui\/bash","ctime":1389845838,"mtime":1389845838,"block_list":"[\"286dc423e71186936c39e3507d2d0cf1\"]","size":2076,"isdir":0,"ifhassubdir":0,"filenum":0}],"request_id":2785468470}

有这么一串,我想利用sed将"isdir":0这里的0单独取出来。利用如下命令:
sed -n 's/"isdir":\(.*\)/\1/p'
发现匹配出来的是:


 {"list":[{"fs_id":4176732013,"path":"\/apps\/zjhui\/bash","ctime":1389845838,"mtime":1389845838,"block_list":"[\"286dc423e71186936c39e3507d2d0cf1\"]","size":2076,0,"ifhassubdir":0,"filenum":0}],"request_id":2785468470}

isdir没了。而我想要的是isdir后面那个0(这个数字也有可能是1)
求解!

sed json 正则表达式 bash Linux

九尾小狐狸 10 years, 6 months ago

这样也行, sed -n 's/.*"isdir":\([01]\).*/\1/p' 。把前后无关的一堆东西都删掉。如果不是0或者1,是个整数,就写成 sed -n 's/.*"isdir":\([0-9]*\).*/\1/p'

红魔馆主D义兄 answered 10 years, 6 months ago

Your Answer