こんにちは、私はawk
2つのテキストファイルをやや具体的な方法でマージしようとしています。 2行をfile1
単語セットfile2
(ただし別々の行にあります)で交互に使用することです。無期限。単語グループはfile2
カンマで区切られます。たとえば、
file1
A Partridge in a Pear Tree
Two Turtle Doves
Three French Hens
Four Calling Birds
Five Gold Rings
Six Geese a-Laying
Seven Swans a-Swimming
Eight Maids a-Milking
Nine Ladies Dancing
Ten Lords a-Leaping
Eleven Pipers Piping
Twelve Drummers Drumming
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore—
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
“’Tis some visitor,” I muttered, “tapping at my chamber door—
Only this and nothing more.”
file2
I was born, on Mars, the red planet
I love frogs, they are so tasty, with, ketchup, I am hungry
結果ファイル
A Partridge in a Pear Tree
Two Turtle Doves
I was born
Three French Hens
Four Calling Birds
on Mars
Five Gold Rings
Six Geese a-Laying
the red planet
Seven Swans a-Swimming
Eight Maids a-Milking
I love frogs
Nine Ladies Dancing
Ten Lords a-Leaping
they are so tasty
Eleven Pipers Piping
Twelve Drummers Drumming
with
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore—
ketchup
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
I am hungry
“’Tis some visitor,” I muttered, “tapping at my chamber door—
Only this and nothing more.”
詳細:
file1
内容に関係なく2行の大連に分かれて- in行には
file2
複数のグループを含めることができます(たとえば、カンマの数に制限はありません)。 - グループは
file2
単語数に制限なく含めることができます(0???を含む)。 file1
長さにfile2
制限はありません。- あるファイルの終わりに達したが他のファイルにまだデータがある場合は、目的の動作が指定されていません。
どうすればいいですか?
ベストアンサー1
awk -F ', *' '!skip {for (i = 1; i <= NF; i++) a[++n] = $i; next}
{print}
FNR % 2 == 0 && m++ < n {print a[m]}
' file2 skip=1 file1