好的,这个问题有两个方面: 1)实际的文件处理位( 2)在unix中循环这个操作。
第1部分)
我有两份文件:
File_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...and将此输出保存到outfile1。
我想我必须使用awk,剪切和/或粘贴之类的东西,但是我不能把它们都放在一起。
第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等。
我现在在巴什工作。提前感谢您的帮助!
发布于 2012-07-20 12:20:45
要使outfile_3(使用E、F),例如:
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-20 14:07:26
这可以通过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_2head -n -1避免在每个output$i文件的末尾有一个单独的分隔符。
发布于 2012-07-20 11:55:59
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中有行即可。
https://stackoverflow.com/questions/11578772
复制相似问题