首页 > 解决方案 > 打印所有行,但连接匹配模式的连续行

问题描述

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

;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

但一直没有成功。

标签: awkconcatenationmatch

解决方案


$ 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 /

推荐阅读