[英]sed : Need to remove all lines except first or last line that matches a string
I have a text file with the following content 我有一个包含以下内容的文本文件
aa : 01
bb : 01
cc : 01
aa : 02
dd : 01
bb : 02
ee : 01
aa : 03
ff : 01
bb : 03
I need to keep first occurrence of line that has string "aa" in it and last occurrence of line that has string "bb" in it. 我需要首先出现其中包含字符串“aa”的行,并且最后一次出现具有字符串“bb”的行。
Please help. 请帮忙。 Output should be 输出应该是
aa : 01
cc : 01
dd : 01
ee : 01
ff : 01
bb : 03
You can use this awk: 你可以使用这个awk:
awk '!c && /^aa/{c=1;print;next} /^bb/{l=$0;next} !/^aa/ {print} END{print l}' file
aa : 01
cc : 01
dd : 01
ee : 01
ff : 01
bb : 03
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.