gpt4 book ai didi

sed - 使用 sed 加入某些行

转载 作者:行者123 更新时间:2023-12-01 21:59:19 25 4
gpt4 key购买 nike

我有一个看起来像这样的输入:

1
2

3
4
5

6

我想用 sed 将它转换为:

12
345
6

我知道可以使用其他工具轻松完成,但我想专门使用 sed 作为学习练习来完成。我试过这个:

sed ':x ; /^ *$/{ N; s/\n// ; bx; }'

但它打印:

123456

有人可以帮我解决这个问题吗?

最佳答案

引自GNU sed manual :

A common technique to process blocks of text such as paragraphs (instead of line-by-line) is using the following construct:

sed '/./{H;$!d} ; x ; s/REGEXP/REPLACEMENT/'
  1. The first expression, /./{H;$!d} operates on all non-empty lines, and adds the current line (in the pattern space) to the hold space. On all lines except the last, the pattern space is deleted and the cycle is restarted.

  2. The other expressions x and s are executed only on empty lines (i.e. paragraph separators). The x command fetches the accumulated lines from the hold space back to the pattern space. The s/// command then operates on all the text in the paragraph (including the embedded newlines).

确实,

sed '/./{H;$!d} ; x ; s/\n//g'

做你想做的。

关于sed - 使用 sed 加入某些行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54299246/

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