gpt4 book ai didi

bash - 如何从文件 bash unix 中剪切几行

转载 作者:行者123 更新时间:2023-12-04 05:04:46 24 4
gpt4 key购买 nike

我有一个文本文件:

Hello
1
2
3
(unknown number of lines)
Hello
(unknown number of lines)
Hello
(unknow number of lines)
Hello
如何在两个第一个“你好”之间切线并将其保存到文件中?
所以输出将是
1
2
3
(unknown number of lines)

最佳答案

使用 awk :

awk '$1=="Hello"{c++;next} c==1' oldfile | tee newfile

要出现第 N 次,请更改计数变量:
awk -v count=1 '$1=="Hello"{c++;next} c==count' oldfile | tee newfile

关于bash - 如何从文件 bash unix 中剪切几行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15671348/

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