2016-07-28 27 views
1

我有一個包含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 

它可以有很多FRAG線將其與圖像線條重新開始之前。我使用的是mac,因此我幾乎可以使用任何工具,但我對vi最爲熟悉。

+0

請看看[編輯的幫助(http://stackoverflow.com/editing-help)。 – Cyrus

+0

@James Brown第一個解決方案效果很好。修改後的版本在第一行之前留下空格。沒有biggie,但我必須刪除它在Excel中或我的列混合起來。 –

回答

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 
} 
+0

使用'next',不要測試所有的值。 –

相關問題