如何编写sed、awk或其他regex one-liner来连接以match开头的连续行?

如何编写sed、awk或其他regex one-liner来连接以match开头的连续行?,regex,awk,sed,Regex,Awk,Sed,我的文件以我希望在连续组中匹配的值开头,然后删除它们之间的新行字符。“连续组”意味着我们只想删除匹配行对之间的换行符。diff提供了一个方便的例子:假设我们想要删除添加行之间的新行字符,即所有以加号开头的行+ 将答案调整为接近,但仅分组成对,而不是继续分组以下所有匹配线: sed '/^+/N;s/\n+/ /' path/to/file.diff (请注意,输入和预期输出还连接以空格开头的行。这是我的一个格式错误,因为已经编写了非常有用的答案来回答输出中表达的意图,所以我保留原样,以免使它们

我的文件以我希望在连续组中匹配的值开头,然后删除它们之间的新行字符。“连续组”意味着我们只想删除匹配行对之间的换行符。diff提供了一个方便的例子:假设我们想要删除添加行之间的新行字符,即所有以加号开头的行
+

将答案调整为接近,但仅分组成对,而不是继续分组以下所有匹配线:

sed '/^+/N;s/\n+/ /' path/to/file.diff
(请注意,输入和预期输出还连接以空格开头的行。这是我的一个格式错误,因为已经编写了非常有用的答案来回答输出中表达的意图,所以我保留原样,以免使它们无效。)

输入示例:

--- some/file/path  2021-02-21 16:33:40.000000000 -0600
+++ another/file/path   2021-02-21 16:33:52.000000000 -0600
@@ -32,7 +32,7 @@
 this
 sentence
-lost
-many
+gained
+several
+other
 words
@@ -91,9 +91,10 @@
 this
 one
-just
-lost
-many
期望输出:

--- some/file/path  2021-02-21 16:33:40.000000000 -0600
+++ another/file/path   2021-02-21 16:33:52.000000000 -0600
@@ -32,7 +32,7 @@
 this sentence
-lost 
-many
+gained several other
 words
@@ -91,9 +91,10 @@
 this one
-just 
-lost 
-many

一个
sed
一个将以
+
字符开头的相邻行连接起来的行:

sed -e ':a' -e '$!N;s/^\(+.*\)\n+/\1 /;ta' -e 'P;D' file

一个
sed
一个将以
+
字符开头的相邻行连接起来的行:

sed -e ':a' -e '$!N;s/^\(+.*\)\n+/\1 /;ta' -e 'P;D' file

像这样的东西应该有用

$ awk '{p=substr($0,1,1); 
        if(p!=pp && pp!="-") printf "\n"; 
        pp=p; 
        printf "%s%s",$0,p=="-"?"\n":""}' file

--- some/file/path  2021-02-21 16:33:40.000000000 -0600
+++ another/file/path   2021-02-21 16:33:52.000000000 -0600
@@ -32,7 +32,7 @@
 this sentence
-lost
-many
+gained+several+other
 words
@@ -91,9 +91,10 @@
 this one
-just
-lost
-many

像这样的东西应该有用

$ awk '{p=substr($0,1,1); 
        if(p!=pp && pp!="-") printf "\n"; 
        pp=p; 
        printf "%s%s",$0,p=="-"?"\n":""}' file

--- some/file/path  2021-02-21 16:33:40.000000000 -0600
+++ another/file/path   2021-02-21 16:33:52.000000000 -0600
@@ -32,7 +32,7 @@
 this sentence
-lost
-many
+gained+several+other
 words
@@ -91,9 +91,10 @@
 this one
-just
-lost
-many
这可能适用于您(GNU-sed):

追加以下行

如果第一行以
+
开头,第二行以相同字符开头,请删除换行符和重复字符,并用空格替换

重复此过程,直到匹配失败

打印/删除第一行并重复。

这可能适用于您(GNU-sed):

追加以下行

如果第一行以
+
开头,第二行以相同字符开头,请删除换行符和重复字符,并用空格替换

重复此过程,直到匹配失败


打印/删除第一行并重复。

awk
解决方案稍微扩展了单行的概念,但按长单行标准来说并不太糟糕,例如:

awk '
    !found && /^+[^+]/ { printf "%s", $0; found=1; next }
    /^[^+]/  { printf (found?"\n%s\n":"%s\n"), $0; found=0; next }
    found    { printf " %s", substr($0,2); next }
             { print }
' file
示例使用/输出

在创造性地命名为
file
的文件中输入内容后,您可以选择将当前目录中的文件复制并用鼠标中键粘贴到xterm中,并具有:

$ awk '
>     !found && /^+[^+]/ { printf "%s", $0; found=1; next }
>     /^[^+]/  { printf (found?"\n%s\n":"%s\n"), $0; found=0; next }
>     found    { printf " %s", substr($0,2); next }
>              { print }
> ' file
--- some/file/path  2021-02-21 16:33:40.000000000 -0600
+++ another/file/path   2021-02-21 16:33:52.000000000 -0600
@@ -32,7 +32,7 @@
this
sentence
-lost
-many
+gained several other
words
@@ -91,9 +91,10 @@
this
one
-just
-lost
-many

注意:您的问题陈述只讨论了以
'+'
开头的行的连接,但您的预期输出也会在
差异
位置信息之后连接前两行。不清楚您是想要一个、另一个还是两者都想要?

这个
awk
解决方案稍微扩展了一个单行程序的概念,但按长单行程序标准来说,这并不太糟糕,例如

awk '
    !found && /^+[^+]/ { printf "%s", $0; found=1; next }
    /^[^+]/  { printf (found?"\n%s\n":"%s\n"), $0; found=0; next }
    found    { printf " %s", substr($0,2); next }
             { print }
' file
示例使用/输出

在创造性地命名为
file
的文件中输入内容后,您可以选择将当前目录中的文件复制并用鼠标中键粘贴到xterm中,并具有:

$ awk '
>     !found && /^+[^+]/ { printf "%s", $0; found=1; next }
>     /^[^+]/  { printf (found?"\n%s\n":"%s\n"), $0; found=0; next }
>     found    { printf " %s", substr($0,2); next }
>              { print }
> ' file
--- some/file/path  2021-02-21 16:33:40.000000000 -0600
+++ another/file/path   2021-02-21 16:33:52.000000000 -0600
@@ -32,7 +32,7 @@
this
sentence
-lost
-many
+gained several other
words
@@ -91,9 +91,10 @@
this
one
-just
-lost
-many

注意:您的问题陈述只讨论了以
'+'
开头的行的连接,但您的预期输出也会在
差异
位置信息之后连接前两行。现在还不清楚您是想要一个,另一个还是两者都想要?

我给您这个--您已经确定了预期的输出!我要给你一个,你完成了预期的输出!啊,是的,谢谢你的留言,我把输出的格式弄错了。:)讨论是准确的半场,只是一种类型的比赛。向上投票!啊,是的,谢谢你的留言,我把输出的格式弄错了。:)讨论是准确的半场,只是一种类型的比赛。向上投票!