sed 将行连接在一起

发布于 2024-12-11 06:15:40 字数 125 浏览 0 评论 0原文

将文件中不以字符“0”结尾的行连接在一起的 sed (或其他工具)命令是什么?

我会有这样的台词

412|n|领先建筑材料

what would be the sed (or other tool) command to join lines together in a file that do not end w/ the character '0'?

I'll have lines like this

412|n|Leader Building Material

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ヤ经典坏疍 2024-12-18 06:15:40

d|d|20||0

需要单独保留,然后我会有像这样的行(有 3 行,但只有一行以 0 结尾)

107|n|打结工具|||||打结工具

|||||d|d|0||0

需要连接/组合成一行

107|n|打结工具|||||打结工具|||||d|d|0||0

d|d|20||0

which need to be left alone, and then I'll have lines like this for example (which is 3 lines, but only one ends w/ 0)

107|n|Knot Tying Tools|||||Knot Tying Tools

|||||d|d|0||0

which need to be joined/combined into one line

107|n|Knot Tying Tools|||||Knot Tying Tools|||||d|d|0||0
回眸一笑 2024-12-18 06:15:40
 sed ':a;/0$/{N;s/\n//;ba}'

在循环中(分支 ba 到标签 :a),如果当前行以 0 结尾 (/0$/) 则追加下一行 ( N)并删除内部换行符(s/\n//)。

awk:

awk '{while(/0$/) { getline a; $0=$0 a; sub(/\n/,_) }; print}'

Perl:

perl -pe '$_.=<>,s/\n// while /0$/'

bash:

while read line; do 
    if [ ${line: -1:1} != "0" ] ; then 
        echo $line
    else echo -n $line
fi
done 
 sed ':a;/0$/{N;s/\n//;ba}'

In a loop (branch ba to label :a), if the current line ends in 0 (/0$/) append next line (N) and remove inner newline (s/\n//).

awk:

awk '{while(/0$/) { getline a; $0=$0 a; sub(/\n/,_) }; print}'

Perl:

perl -pe '$_.=<>,s/\n// while /0$/'

bash:

while read line; do 
    if [ ${line: -1:1} != "0" ] ; then 
        echo $line
    else echo -n $line
fi
done 
岁吢 2024-12-18 06:15:40

awk 也可能很短:

awk '!/0$/{printf $0}/0$/'

测试:

kent$  cat t
#aasdfasdf
#asbbb0
#asf
#asdf0
#xxxxxx
#bar

kent$  awk '!/0$/{printf $0}/0$/' t
#aasdfasdf#asbbb0
#asf#asdf0
#xxxxxx#bar 

awk could be short too:

awk '!/0$/{printf $0}/0$/'

test:

kent$  cat t
#aasdfasdf
#asbbb0
#asf
#asdf0
#xxxxxx
#bar

kent$  awk '!/0$/{printf $0}/0$/' t
#aasdfasdf#asbbb0
#asf#asdf0
#xxxxxx#bar 
抠脚大汉 2024-12-18 06:15:40

这个答案的评级令人惊讶;s(这个关于 sed 替换的惊讶眨眼表情双关语是故意的)给定OP规范:sed将行连接在一起

提交的最后一条评论

“如果是这种情况,请检查 @ninjalj 提交的内容”

还建议检查相同的答案

IE。使用 sed ':a;/0$/{N;s/\n//;ba}' 逐字检查

sed ':a;/0$/{N;s/\n//;ba}'
 does
 no one
 ie. 0
 people,
 try
 nothing,

 ie. 0
 things,
 any more,
 ie. 0
 tests?

          (^D aka eot 004 ctrl-D ␄  ... bash generate via: echo ^V^D)

,这不会给出(进行测试;):

 does no one ie. 0
 people, try nothing, ie. 0
 things, any more, ie. 0
 tests?          (^D aka eot 004 ctrl-D ␄  ... bash generate via: echo ^V^D)

要获得此用途:

sed 'H;${z;x;s/\n//g;p;};/0$/!d;z;x;s/\n//g;'

或:

sed ':a;/0$/!{N;s/\n//;ba}'

not:

sed ':a;/0$/{N;s/\n//;ba}'

注意:

sed 'H;${x;s/\n//g;p;};/0$/!d;z;x;s/\n//g;'

不使用分支和
等同于:

sed '${H;z;x;s/\n//g;p;};/0$/!{H;d;};/0$/{H;z;x;s/\n//g;}'
  • H 开始所有序列
  • d 短路当前行上的进一步脚本命令执行并开始下一个周期,因此地址选择器遵循 /0$/! 只能是/0$/!! 所以
    的地址选择器
    /0$/{H;z;x;s/\n//g;} 是多余的,不需要。
  • 如果一行不以 0 结尾,则将其保存在保留空间
    /0$/!{H;d;}
  • 如果一行确实以 0 结尾,也保存它,然后打印flush(双关语,即清除并对齐行)
    /0$/{H;z;x;s/\n//g;}
  • 注意 ${H;z;x;s/\n//g;p;} 使用 /0$/ ... 命令,并带有额外的 p 来强制最终打印,并带有现在不必要的 z (清空并重置模式空间,如 s/.*//)

The rating of this answer is surprising ;s (this surprised wink emoticon pun on sed substitution is intentional) given the OP specifications: sed join lines together.

This submission's last comment

"if that's the case check what @ninjalj submitted"

also suggests checking the same answer.

ie. Check using sed ':a;/0$/{N;s/\n//;ba}' verbatim

sed ':a;/0$/{N;s/\n//;ba}'
 does
 no one
 ie. 0
 people,
 try
 nothing,

 ie. 0
 things,
 any more,
 ie. 0
 tests?

          (^D aka eot 004 ctrl-D ␄  ... bash generate via: echo ^V^D)

which will not give (do the test ;):

 does no one ie. 0
 people, try nothing, ie. 0
 things, any more, ie. 0
 tests?          (^D aka eot 004 ctrl-D ␄  ... bash generate via: echo ^V^D)

To get this use:

sed 'H;${z;x;s/\n//g;p;};/0$/!d;z;x;s/\n//g;'

or:

sed ':a;/0$/!{N;s/\n//;ba}'

not:

sed ':a;/0$/{N;s/\n//;ba}'

Notes:

sed 'H;${x;s/\n//g;p;};/0$/!d;z;x;s/\n//g;'

does not use branching and
is identical to:

sed '${H;z;x;s/\n//g;p;};/0$/!{H;d;};/0$/{H;z;x;s/\n//g;}'
  • H commences all sequences
  • d short circuits further script command execution on the current line and starts the next cycle so address selectors following /0$/! can only be /0$/!! so the address selector of
    /0$/{H;z;x;s/\n//g;} is redundant and not needed.
  • if a line does not end with 0 save it in hold space
    /0$/!{H;d;}
  • if a line does end with 0 save it too and then print flush (double entendre ie. purged and lines aligned)
    /0$/{H;z;x;s/\n//g;}
  • NB ${H;z;x;s/\n//g;p;} uses /0$/ ... commands with an extra p to coerce the final print and with a now unnecessary z (to empty and reset pattern space like s/.*//)
薄暮涼年 2024-12-18 06:15:40

典型的神秘 Perl 单行:

perl -pe 'BEGIN{$/="0\n"}s/\n//g;$_.=$/'

它使用序列“0\n”作为记录分隔符(根据你的问题,我假设每一行都应该以零结尾)。任何记录都不应该有内部换行符,因此它们被删除,然后打印该行,附加删除的 0 和换行符。

对你的问题的另一个看法是确保每行有 17 个管道分隔的字段。这并不假设第 17 个字段值必须为零。

awk -F \| '
    NF == 17 {print; next}
    prev {print prev $0; prev = ""}
    {prev = $0}
'

A typically cryptic Perl one-liner:

perl -pe 'BEGIN{$/="0\n"}s/\n//g;$_.=$/'

This uses the sequence "0\n" as the record separator (by your question, I'm assuming that every line should end with a zero). Any record then should not have internal newlines, so those are removed, then print the line, appending the 0 and newline that were removed.

Another take to your question would be to ensure each line has 17 pipe-separated fields. This does not assume that the 17th field value must be zero.

awk -F \| '
    NF == 17 {print; next}
    prev {print prev $0; prev = ""}
    {prev = $0}
'
以往的大感动 2024-12-18 06:15:40

如果以 0 存储结尾,则删除换行符..

sed '/0$/!N;s/\n//'

if ends with 0 store, remove newline..

sed '/0$/!N;s/\n//'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文