Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何根据正则表达式搜索删除行数?例如:如果我有一个文件:
aaa bbb ccc ddd bbb eee
我想删除包含“bbb”字符串的行和它之后的行。所以结果应该是这样的:
aaa ddd
addr1,+N将匹配addr1和N后面的行addr1。
addr1,+N
addr1
N
$ sed '/bbb/,+1d' file.txt aaa ddd
试试这个,
sed '/bbb/{N;d;}' your_file.txt
printf "aaa\nbbb\nccc\nddd\neee" | sed '/bbb/,1+d'
输出
aaa ddd eee