如何在匹配时仅使用 sed 有条件地删除第一行? [英] How to conditionally remove first line only with sed when it matches?
本文介绍了如何在匹配时仅使用 sed 有条件地删除第一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用 sed
来检查某些命令输出的第一行(到 stdout),如果它与某个模式匹配,则删除第一行吗?
Can I use sed
to check the first line of some command's output (to stdout) and delete this very first line if it matches a certain pattern?
比如说,命令的输出是这样的:
Say, the command's output is something like this:
"AB"
"CD"
"E"
"F"
我希望它变成:
"CD"
"E"
"F"
但是当第一行是"GH"
时,我不想删除该行.
But when the first line is "GH"
, I don't want to delete the line.
我试过了,但没有用:
<some_command> |sed '1/<pattern>/d'
外壳告诉我:
sed: 0602-403 1/<pattern>/d is not a recognized function.
我只想使用 sed 处理第一行,其他行保持不变.
I only want to use sed to process the first line, leaving the other lines untouched.
这里的正确语法是什么?
What is the correct syntax here?
推荐答案
这可能对你有用:
sed -e '1!b' -e '/GH/!d' file
这篇关于如何在匹配时仅使用 sed 有条件地删除第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文