合并两个文件:两行,部分行,两行,部分行等

合并两个文件:两行,部分行,两行,部分行等

您好,我正在尝试以awk一种相当特殊的方式合并两个文本文件,从 中取出两行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分为两行对联,不分内容
  • in 中的一行file2可以有任意数量的组(即任意数量的逗号)
  • 一个组file2可以有任意数量的单词(包括零???)
  • 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

答案2

file2猜测您想在每 2 行之后的逗号之间插入句子file1,您可以尝试以下awk脚本:

 awk -F", *" 'NR==FNR{
                 for(i=1;i<NF+1;i++)
                    a[i]=$i
              } 
              NR>FNR{
                 print; 
                 if(FNR%2==0) 
                     print a[FNR/2]
              }' file2 file1

答案3

awkRecord Separator (这里假设 GNUawk或最新版本的mawk

awk '{print}!(NR%2){getline <"file2";print}' RS="\n|, " file1

如果有,文件1的行,更正确的版本可能是:

awk 'BEGIN{r=RS}{print}!(NR%2){RS=r"|, ";getline <"file2";print;RS=r}' file1

修改后的问题可以通过以下方式解决(可移植)

awk '{print};!(NR%2) && (getline <"file2")>0{gsub(", *", "\n");print}' file1

答案4

假设@oliv的解释是正确的,这个解决方案也可能有效,尽管它不使用awk

paste -d '\n ' file1 <(sed  's/^/\n/;s/, */\n\n/g' file2) | sed '/^$/d'

鉴于更新根据OP的要求,这似乎不再是一个可行的方法。

相关内容