gpt4 book ai didi

linux - 有什么方法可以通过 sed 删除第一次出现的模式吗?

转载 作者:太空宇宙 更新时间:2023-11-04 09:07:18 25 4
gpt4 key购买 nike

这是一个测试文件:

xxx
hello
hello
others

我想使用 sed:

sed -i '/hello/d' the_test_file

这将删除所有包含“hello”的行,那么如何只删除包含“hello”的第一行呢?

我认为 sed 做不到,但是使用 perl 我可以。就像:

perl -00 -pe "s/hello//" the_test_file > /tmp/file && mv /tmp/file the_test_file

最佳答案

显示文件的原始内容。

cat the_test_file
1
2
3
popo
hello 1
hello 2
others

运行sed命令后显示结果

sed -e '0,/^hello/{//d;}' the_test_file
1
2
3
popo
hello 2
others

最后回答你的问题

注意:感谢 jaypal

sed -i"bak" -e '0,/^hello/{//d;}' the_test_file

关于linux - 有什么方法可以通过 sed 删除第一次出现的模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8335573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com