gpt4 book ai didi

linux - 用awk解析多行

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:12 24 4
gpt4 key购买 nike

我有一个多行输出,像这样:

foo: some text
goes here
and here
and here
bar: more text
goes here
and here
xyz: and more...
and more...
and more...

文本的格式与这里显示的完全一样。我感兴趣的文本的“组/节”在行首之后开始,并在下一个文本开始之前的行结束。

在这个例子中,grouls 是 foobar 之前的所有文本。然后是 barxyz 之前的所有文本。最后,xyz 直到结束。

最佳答案

输入

$ cat file
foo: some text
goes here
and here
and here
bar: more text
goes here
and here
xyz: and more...
and more...
and more...

输出

$ awk '/:/{f=/^foo/}f' file
foo: some text
goes here
and here
and here

如果您想跳过匹配的行

$ awk '/:/{f=/^foo/;next}f' file
goes here
and here
and here

甚至

# Just modify variable search value
# 1st approach
$ awk -v search="foo" '/:/{f=$0~"^"search}f' file
foo: some text
goes here
and here
and here

# 2nd approach
$ awk -v search="foo" '/:/{f=$0~"^"search;next}f' file
goes here
and here
and here

关于linux - 用awk解析多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42350342/

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