回复 20# qixiaobin0715
相对于纯P,俺更喜欢第3方工具,如gawk、sed:
1.删除两匹配行([222],[333])之间的行,两匹配行都保留gawk "/\[222\]/{print;while((getline)>0)if(/\[333\]/){print;next}}1" 1.txt>2.txtCOPY
sed -i -r -e "/\[222\]/,/\[333\]/{/\[222\]/p;/\[333\]/p;d;}" 1.txtCOPY 2.保留首匹配行gawk "/\[222\]/{print;while((getline)>0)if(/\[333\]/)next}1" 1.txt>2.txtCOPY
sed -i -r -e "/\[222\]/,/\[333\]/{/\[222\]/p;d;}" 1.txtCOPY 3.保留末匹配行gawk "/\[222\]/{while((getline)>0)if(/\[333\]/){print;next}}1" 1.txt>2.txtCOPY
sed -i -r -e "/\[222\]/,/\[333\]/{/\[333\]/p;d;}" 1.txtCOPY 4.两匹配行都不保留gawk "/\[222\]/,/\[333\]/{next}1" 1.txt>2.txtCOPY
sed -i -r -e "/\[222\]/,/\[333\]/d" 1.txtCOPY
|