我有兩個文本文件,每個文本文件都包含一個由空行分隔的文本塊。塊的大小各不相同。按塊合併兩個文本文件
# ::id 10
# ::snt Yes !
...multiple lines of unstructured data from file 1...
# ::id 11
# ::snt said Lion .
...multiple lines of unstructured data from file 1...
# ::id 12
# ::snt Yes yes !
...multiple lines of unstructured data from file 1...
# ::id 13
# ::snt said Tiger .
...multiple lines of unstructured data from file 1...
,同樣另一
# ::id 10
# ::snt No !
...multiple lines of unstructured data from file 2...
# ::id 11
# ::snt said Monkey .
...multiple lines of unstructured data from file 2...
# ::id 12
# ::snt No no !
...multiple lines of unstructured data from file 2...
# ::id 13
# ::snt said Donkey .
...multiple lines of unstructured data from file 2...
我要合併的兩個街區,但他們的# ::id
排序。另外,我需要在file2數據塊之前保留file1數據塊的順序。所以最終輸出應該是這樣的:
# ::id 10
# ::snt Yes !
...multiple lines of unstructured data from file 1...
# ::id 10
# ::snt No !
...multiple lines of unstructured data from file 2...
# ::id 11
# ::snt said Lion .
...multiple lines of unstructured data from file 1...
# ::id 11
# ::snt said Monkey .
...multiple lines of unstructured data from file 2...
# ::id 12
# ::snt Yes yes !
...multiple lines of unstructured data from file 1...
# ::id 12
# ::snt No no !
...multiple lines of unstructured data from file 2...
# ::id 13
# ::snt said Tiger .
...multiple lines of unstructured data from file 1...
# ::id 13
# ::snt said Donkey .
...multiple lines of unstructured data from file 2...
我該怎麼做?任何將工作bash
,sed
,awk
'awk'允許您使用正則表達式作爲記錄分隔符(RS)。例如,您可以將文件讀取到數組中,並使用'asort'對其進行排序。 –
您能否提供語法?我的'awk'有點生疏。問題是我想將這兩個文本文件合併爲一個。我怎麼做? – Sudhi
基本思想是將兩個文件讀入數組並對其進行排序。但是,如果文件非常大,那將不是有效的策略。 –