Awk 打印包含图案的行,连接下一行,直到找到具有图案的下一行

Awk 打印包含图案的行,连接下一行,直到找到具有图案的下一行,awk,sed,Awk,Sed,数据: 所需输出: awk '/START/{if (NR!=1)print "";next}{printf $0}END{print "";}' file | 3 |这是第一步。这描述了第一步。有许多第一步需要遵循,正如您所看到的,可以进行多行操作。 |7 |这是第八步。这描述了你需要知道和练习的内容。有许多这样的步骤需要遵循,你可以看到它可以持续几行。 |14 |这是第十一步。这描述了如何在awk中编写代码。有许多这样的步骤需要遵循,可以持续几行,如您所见。 请尝试以下内容 <STA

数据:

所需输出:

awk '/START/{if (NR!=1)print "";next}{printf $0}END{print "";}' file
| 3 |这是第一步。这描述了第一步。有许多第一步需要遵循,正如您所看到的,可以进行多行操作。
|7 |这是第八步。这描述了你需要知道和练习的内容。有许多这样的步骤需要遵循,你可以看到它可以持续几行。
|14 |这是第十一步。这描述了如何在awk中编写代码。有许多这样的步骤需要遵循,可以持续几行,如您所见。

请尝试以下内容

<START>|3|This is the first step.This describes the first step.There are many first steps to be followed and it can go on for multiple lines as you can see.
<START>|7|This is the eighth step.This describes what you need to know and practice.There are many such steps to be followed and it can go on for several lines as you can see.
<START>|14|This is the eleventh step.This describes how to write a code in awk.There are many such steps to be followed and it can go on for several lines as you can see.
awk'
/^/{
如果(值){
打印值
value=“”
}
值=$0
下一个
}
{
值=(值?s的值:)$0
}
结束{
如果(值){
打印值
}
}'输入文件

请尝试以下内容

<START>|3|This is the first step.This describes the first step.There are many first steps to be followed and it can go on for multiple lines as you can see.
<START>|7|This is the eighth step.This describes what you need to know and practice.There are many such steps to be followed and it can go on for several lines as you can see.
<START>|14|This is the eleventh step.This describes how to write a code in awk.There are many such steps to be followed and it can go on for several lines as you can see.
awk'
/^/{
如果(值){
打印值
value=“”
}
值=$0
下一个
}
{
值=(值?s的值:)$0
}
结束{
如果(值){
打印值
}
}'输入文件
这可能适合您(GNU-sed):

关闭自动打印
-n

如果一行不包含开始,请将其附加到保留空间,如果它不是当前循环中文件中断的最后一行

否则,切换到保留空间并用空格替换所有换行符,如果成功,则打印结果

注意:通过在行累积后交换到保持空间,当前行成为新保持空间的第一行。此外,解决方案将换行符替换为空格,如果这不是理想的结果,则可以使用以下方法删除换行符:

 sed -n '/START/!{H;$!b};x;s/\n/ /gp' file
这可能适用于您(GNU-sed):

关闭自动打印
-n

如果一行不包含开始,请将其附加到保留空间,如果它不是当前循环中文件中断的最后一行

否则,切换到保留空间并用空格替换所有换行符,如果成功,则打印结果

注意:通过在行累积后交换到保持空间,当前行成为新保持空间的第一行。此外,解决方案将换行符替换为空格,如果这不是理想的结果,则可以使用以下方法删除换行符:

 sed -n '/START/!{H;$!b};x;s/\n/ /gp' file
$awk'{printf“%s%s”,(/^/?sep:),$0;sep=ORS}结束{print”“}文件
|3 |这是第一步。这描述了第一步。有许多第一步需要遵循,正如您所看到的,它可以持续多行。
|7 |这是第八步。这描述了你需要知道和练习的内容。有许多这样的步骤需要遵循,你可以看到它可以持续几行。
|14 |这是第十一步。这描述了如何在awk中编写代码。有许多这样的步骤需要遵循,可以持续几行,如您所见。
或者,如果您愿意(例如,如果您需要在打印前建立完整的处理记录):

$awk'/^/{if(NR>1)print rec;rec=”“}{rec=rec$0}END{print rec}文件
|3 |这是第一步。这描述了第一步。有许多第一步需要遵循,正如您所看到的,它可以持续多行。
|7 |这是第八步。这描述了你需要知道和练习的内容。有许多这样的步骤需要遵循,你可以看到它可以持续几行。
|14 |这是第十一步。这描述了如何在awk中编写代码。有许多这样的步骤需要遵循,可以持续几行,如您所见。
这两种方法都适用于每个UNIX设备上任何shell中的任何awk。

$awk'{printf“%s%s”,(/^/?sep:),$0;sep=ORS}END{print”“}文件 |3 |这是第一步。这描述了第一步。有许多第一步需要遵循,正如您所看到的,它可以持续多行。 |7 |这是第八步。这描述了你需要知道和练习的内容。有许多这样的步骤需要遵循,你可以看到它可以持续几行。 |14 |这是第十一步。这描述了如何在awk中编写代码。有许多这样的步骤需要遵循,可以持续几行,如您所见。 或者,如果您愿意(例如,如果您需要在打印前建立完整的处理记录):

$awk'/^/{if(NR>1)print rec;rec=”“}{rec=rec$0}END{print rec}文件
|3 |这是第一步。这描述了第一步。有许多第一步需要遵循,正如您所看到的,它可以持续多行。
|7 |这是第八步。这描述了你需要知道和练习的内容。有许多这样的步骤需要遵循,你可以看到它可以持续几行。
|14 |这是第十一步。这描述了如何在awk中编写代码。有许多这样的步骤需要遵循,可以持续几行,如您所见。

这两种方法都适用于每个UNIX设备上任何shell中的任何awk。

欢迎使用,请务必用简单的话提及示例输入和示例预期输出。另外,根据论坛规则,将您的示例/代码始终包装在代码标签中。示例输入粘贴在每个句子末尾,其中包含换行符。此外,每条记录的模式从第一行开始。输出应该只在记录的末尾有换行符。这通常是以开始模式开始的下一条记录之前的行。每个记录只有三个字段。第一个字段是模式开始。第二个是数字。第三是一个包含新词的多行句子。我们的目标是只在每条记录的末尾保留换行符。请将示例输入(无描述,无图像)和您对该示例输入的期望输出添加到您的问题中(无评论)。您可以在代码标记中的帖子中提及您的示例输入和期望输出的几行。欢迎使用SO,请务必用简单的话提及样本输入和样本预期输出。另外,根据论坛规则,将您的样本/代码始终包装在代码标签中。粘贴的样本输入包含
 sed -n '/START/!{H;$!b};x;s/\n//gp' file
$ awk '{printf "%s%s", (/^<START>/ ? sep : ""), $0; sep=ORS} END{print ""}' file
<START>|3|This is the first step.This describes the first step.There are many first steps to be followed and it can go on for multiple lines as you can see.
<START>|7|This is the eighth step.This describes what you need to know and practice.There are many such steps to be followed and it can go on for several lines as you can see.
<START>|14|This is the eleventh step.This describes how to write a code in awk.There are many such steps to be followed and it can go on for several lines as you can see.
$ awk '/^<START>/{if (NR>1) print rec; rec=""} {rec = rec $0} END{print rec}' file
<START>|3|This is the first step.This describes the first step.There are many first steps to be followed and it can go on for multiple lines as you can see.
<START>|7|This is the eighth step.This describes what you need to know and practice.There are many such steps to be followed and it can go on for several lines as you can see.
<START>|14|This is the eleventh step.This describes how to write a code in awk.There are many such steps to be followed and it can go on for several lines as you can see.