根据第一个字段patern

时间:2016-07-28 04:20:58

标签: bash shell awk sed vi

我有一个200,000行的文件。每行的开头以“IMAGE”,“HISTO”或“FRAG”开头。我需要将线条HISTO和FRAG连接到IMAGE线。这是一个例子。

IMAGE Lots of Data on this line  
HISTO usually numbers 0 0 1 1 0 1 0  
FRAG Always at least 1 of these lines but can be more

结果需要如下所示:

>IMAGE Lots of Data on this line HISTO usually numbers 0 0 1 1 0 1 0 FRAG Always at least 1 of these lines but can be more

在使用IMAGE线重新开始之前,可以有许多FRAG线。我正在使用mac所以我可以使用几乎任何工具,但我最熟悉vi。

1 个答案:

答案 0 :(得分:4)

AWK:

awk '/^IMAGE/&&NR>1 {print a; a=""} {a=a""$0" "} END{print a}' test.in

大声说道:

/^IMAGE/ && NR>1 { # if it starts with IMAGE
    print a        # empty buffer variable to output
    a=""           # reset the buffer after emptying
} 
{                  # for all records
  a=a""$0" "       # append to the buffer variable, prob. no need for ""
}
END {              # in the end
  print a          # empty the remaining buffer in the end
}
相关问题