gpt4 book ai didi

sed - 使用 sed 仅打印每个段落的第一个单词

转载 作者:行者123 更新时间:2023-12-04 17:36:45 26 4
gpt4 key购买 nike

我想知道如何使用 sed one-liner 仅打印出每个段落的第一个单词。在这种情况下,段落由跟随 2 个换行符的文本定义。

例如

This is a paragraph with some text. Some random text that is not really important.

This is another paragraph with some text.
However this sentence is still in the same paragraph.

这应该转换为
This

This

最佳答案

思考
paragraph mode

By a special dispensation, an empty string as the value of RS indicates that records are separated by one or more blank lines. 

awk or perl have support for a 'paragraph mode' and either would make a better choice than sed:

awk '{ print $1 }' RS= ORS="\n\n" file

或者

perl -00 -lane 'print $F[0]' file

结果:
This

This

关于sed - 使用 sed 仅打印每个段落的第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16385720/

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