Awk 打印所有行,但连接与图案匹配的连续行

Awk 打印所有行,但连接与图案匹配的连续行,awk,concatenation,match,Awk,Concatenation,Match,我有一个包含以下数据的文件: ;Citation1 begins here and contains characters including , . and numbers DATA 1 259.85 101000 0.094837707 0.9089 / 2 266.07 101000 0.097842938 0.8997 / 3 270.95 101000 0.105071894 0.8899 / 4 273.35 101000

我有一个包含以下数据的文件:

;Citation1 begins here and contains characters including , . and numbers

DATA 1 259.85 101000 0.094837707 0.9089 / 
         2 266.07 101000 0.097842938 0.8997 / 
         3 270.95 101000 0.105071894 0.8899 / 
         4 273.35 101000 0.112016587 0.8849 / 
         5 278.75 101000 0.134569045 0.87 / 

;Citation2 begins here and contains characters including , . and numbers but
;this one continues on the next line

DATA 1 259.85 101000 0.094837707 0.9089 / 
         2 266.07 101000 0.097842938 0.8997 / 
         3 270.95 101000 0.105071894 0.8899 / 
         4 273.35 101000 0.112016587 0.8849 / 
         5 278.75 101000 0.134569045 0.87 / 
我想把所有的行打印成一个新文件。但是,如果连续行以相同字符开头(此处为“;”),我希望将它们连接到同一行。因此,上述输入文件将显示为:

;Citation1 begins here and contains characters including , . and numbers

DATA 1 259.85 101000 0.094837707 0.9089 / 
         2 266.07 101000 0.097842938 0.8997 / 
         3 270.95 101000 0.105071894 0.8899 / 
         4 273.35 101000 0.112016587 0.8849 / 
         5 278.75 101000 0.134569045 0.87 / 

;Citation2 begins here and contains characters including , . and numbers but this one continues on the next line

DATA 1 259.85 101000 0.094837707 0.9089 / 
         2 266.07 101000 0.097842938 0.8997 / 
         3 270.95 101000 0.105071894 0.8899 / 
         4 273.35 101000 0.112016587 0.8849 / 
         5 278.75 101000 0.134569045 0.87 / 
我尝试过使用不同的awk命令变体,例如:

awk '/;/ && last {printf "%s","\n"last;$0}{printf "%s",$0}END{print} /;/{last=$0}' input.txt > output.txt

但都没有成功

欢迎来到SO,on SO,我们鼓励用户添加他们为解决自己的问题所付出的努力,因此请添加同样的努力,然后让我们知道。为什么不集中于三条规则?第一个查找
/^/
(连接所有行,从char 2开始,递增计数器
n
跳到下一条记录)下一条检查
n>0
(打印前缀为
;“
的连接行,并使用附加的
'\n'
,重置字符串和计数器,跳到下一条)和最终规则
1
(默认打印)。试试看,让我们知道你在哪里遇到了麻烦。我编辑了这篇文章来回应第一条评论。我尝试了一百万种不同的路线,但没有成功。通过第二条评论,我理解了逻辑,但我仍在试图找出实现它的正确语法。谢谢@用户13758913干得好。你可以看到我会在Ed的答案下面发布的内容(在完美的
awk
解决方案上很难与Ed竞争)。访问评论中提供的链接,调查并纠正数据文件中存在的任何杂散线路终止问题。这两种解决方案都使用从您的问题中复制和粘贴的输入来满足您的要求。好吧,这就是
awk'/^;/{s=substr($0,2);cite=(n>0)?cite“s:s;n++;next};n> 0{print”;“cite;cite=”“;n=0;next}1'文件
谢谢!两者都很好。但是,有没有一种方法可以连接第二行文本,使其显示在前一行中。现在,它们仍然出现在两行中,但第二行缺少“;”正如你在我的回答中看到的那样,这并没有发生,它完全符合你的要求。如果你得到了不同的结果,那么要么你复制/粘贴了错误的脚本,要么你的实际输入与你发布的示例不符。我相信你所说的第二种可能性就是正在发生的事情。我想我的输入文件在第一行引用的末尾有一些东西。我不确定它可能是什么,因为它看起来就像一条新线。无论如何,我会努力解决这个问题。谢谢你的帮助,不客气。这可能是一个CR。看看这个问题,看看下一步该怎么做。
$ awk '
    {
        curr = $0
        printf "%s%s", ( (prev ~ /^;/) && sub(/^;/,"") ? OFS : ors ), $0
        ors = ORS
        prev = curr
    }
    END { print "" }
' file
;Citation1 begins here and contains characters including , . and numbers

DATA 1 259.85 101000 0.094837707 0.9089 /
         2 266.07 101000 0.097842938 0.8997 /
         3 270.95 101000 0.105071894 0.8899 /
         4 273.35 101000 0.112016587 0.8849 /
         5 278.75 101000 0.134569045 0.87 /

;Citation2 begins here and contains characters including , . and numbers but this one continues on the next line

DATA 1 259.85 101000 0.094837707 0.9089 /
         2 266.07 101000 0.097842938 0.8997 /
         3 270.95 101000 0.105071894 0.8899 /
         4 273.35 101000 0.112016587 0.8849 /
         5 278.75 101000 0.134569045 0.87 /