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