sed正则经典案例(三)
###打印指定的行内容
已知文件内容如下:
cat index.txt:test{ admin:111 index:aaa admin:111}normal{ admin:3333 index:bbb admin:3333}test{ admin:111 index:ccc admin:111}
要求打印 test 段落里的 index 后面的值:
aaaccc
解答:多种方法汇总
sed -rn '{/index/s#.*:([^b]+)#\1#p}' index.txtsed -rn '/^test/,+2s/.*index:(.*)/\1/p' index.txtsed -nr '1~2s#index:(.+)#\1#p' index.txtsed -r '/[1-3]+|^.*\{|\}|^.*b/d' index.txt |cut -d ":" -f2sed -r '/[1-3]+|^.*\{|\}/d' index.txt|awk -F ":" '{print $2}'|sed '/^.*b/d'awk -F':' '/^test\{/{a=1}/\}/{a=0}a&&/index/{print $2}' index.txt