2

好的,这个问题有两个方面:1)实际的文件操作位 2)在 unix 中循环这个操作

第1部分)

我有两个文件:

文件_1

a b
c d
e f
g h

和 File_2

A B
C D
E F
G H
I J

我想(首先)得到以下结果:

a b
A B
>
c d
A B
>
e f
A B
>
g h
A B

...并将此输出保存到 outfile1。

我收集我将不得不使用诸如awkcut和/或paste之类的东西,但我无法将它们放在一起。

第2部分)

然后我想对 File_2 中的所有行循环这个操作(注意 File_1 中的行数与 File_2 中的不同),这样我最终得到 5 个输出文件,其中 outfile2 将是:

a b
C D
>
c d
C D
>
e f
C D
>
g h
C D

和 outfile3 将是:

a b
E F
>
c d
E F
>
e f
E F
>
g h
E F

等等

目前我正在使用 bash。预先感谢您的任何帮助!

4

4 回答 4

2

这可以通过bash重定向来完成:

i=1

while read f2; do
  while read f1; do
    echo "$f1"
    echo "$f2"
    echo ">"
  done < File_1 | head -n -1 > output$i
  (( i++ ))
done < File_2

head -n -1output$i避免在每个文件的末尾有一个单独的分隔符。

于 2012-07-20T14:07:26.700 回答
0
sort -m -f file1 file2 | uniq -i --all-repeated=separate

看着挺近的。但是,在第二次阅读时,我认为您想要更像这个 perl 脚本的东西:

use strict;
use warnings;

open(my $FILE1, '<file1') or die;

my $output = 0;

while (my $a = <$FILE1>)
{
    $output++;
    open(my $OUT, ">output$output");
    open(my $FILE2, '<file2') or die;

    print $OUT "$a$_---\n" foreach (<$FILE2>);

    close $FILE2;
    close $OUT;
}

close $FILE1;

这将创建输出文件output1, output12, output3, output..., 与 file1 中的行数一样多

于 2012-07-20T11:55:59.267 回答
0

制作 outfile_3(with EF) 例如:

x=$(sed -n '3p' File_2)
awk "{ printf \"%s\\n%s\\n>\\n\", \$0, \"$x\" }" File_1 > outfile_3

在第一行'3p'削减第三行
现在让我们循环执行:

(( i = 1 ))
while read line
do
  awk "{ printf \"%s\\n%s\\n>\\n\", \$0, \"$line\" }" File_1 > "outfile_$i"
  (( i++ ))
done < File_2
于 2012-07-20T12:20:45.813 回答
0

一个 awk 一班轮:

     awk 'NR==FNR{a[NR]=$0;l=NR;next;} {b[FNR]=$0;}
    END{f=1; for(x=1;x<=FNR;x++){for(i=1;i<=length(a);i++){
printf "%s\n%s\n%s\n", a[i],b[x],">" > "output"f }f++;}}' f1 f2

测试:

kent$  head f1 f2
==> f1 <==
a b
c d
e f
g h

==> f2 <==
A B
C D
E F
G H
I J


kent$  awk 'NR==FNR{a[NR]=$0;l=NR;next;} {b[FNR]=$0;}
END{f=1; for(x=1;x<=FNR;x++){for(i=1;i<=length(a);i++){printf "%s\n%s\n%s\n", a[i],b[x],">" > "output"f }f++;}}' f1 f2

kent$  head -30 out*
==> output1 <==
a b
A B
>
c d
A B
>
e f
A B
>
g h
A B
>

==> output2 <==
a b
C D
>
c d
C D
>
e f
C D
>
g h
C D
>

==> output3 <==
a b
E F
>
c d
E F
>
e f
E F
>
g h
E F
>

==> output4 <==
a b
G H
>
c d
G H
>
e f
G H
>
g h
G H
>

==> output5 <==
a b
I J
>
c d
I J
>
e f
I J
>
g h
I J
>
于 2012-07-20T12:58:26.497 回答