我有两个文件,需要从file1
的每一行中的字符位置92-97中抓取文本,然后用它来替换{{1}}中相应行的相同部分。
例如:
File1中
file2
文件2
LOCAR0013LOCBLKAR0013LOCDEFAR0013LOC024500230012BLKAR0013 7Q3G013 003340000000000000
LOCAR0030LOCBLKAR0030LOCDEFAR0030LOC091500960004BLKAR0030 M20G010 000165000000000000
LOCAR0031LOCBLKAR0031LOCDEFAR0031LOC024500230012BLKAR0031 M21G011 002240000000000000
预期输出到新文件
LOCAR0013LOCBLKAR0013LOCDEFAR0013LOC024500230012BLKAR0013 7Q3G4GR 000040000000000000
LOCAR0030LOCBLKAR0030LOCDEFAR0030LOC091500960004BLKAR0030 M20GTRS 000105000000000000
LOCAR0031LOCBLKAR0031LOCDEFAR0031LOC024500230012BLKAR0031 M21G2AS 000040000000000000
我在Unix / Solaris系统上有这个。
答案 0 :(得分:1)
这应该做你需要的事情
use strict;
use warnings;
use autodie;
my ($file1, $file2) = @ARGV;
open my $fh1, '<', $file1;
open my $fh2, '<', $file2;
while (my $line2 = <$fh2>) {
my $line1 = <$fh1>);
if (defined $line1) {
substr($line2, 91, 6) = substr($line1, 91, 6);
}
print $line2;
}
答案 1 :(得分:0)
怎么样:
$ paste file1 file2 | awk '{print $1,$5,$3}' OFS='\t\t'
LOCAR0013LOCBLKAR0013LOCDEFAR0013LOC024500230012BLKAR0013 7Q3G4GR 003340000000000000
LOCAR0030LOCBLKAR0030LOCDEFAR0030LOC091500960004BLKAR0030 M20GTRS 000165000000000000
LOCAR0031LOCBLKAR0031LOCDEFAR0031LOC024500230012BLKAR0031 M21G2AS 002240000000000000
答案 2 :(得分:0)
假设您在perl中将文件加载到内存中:
my @file1 = <$fh1>;
my @file2 = <$fh2>;
for my $line (0 .. $#file1) {
my $replace = substr($file1[$line], 91, 6); # take your string from file1
substr($file2[$line], 91, 6) = $replace; # put it into file2
}
现在,您将在@file2
中更改了行。
substr
函数也可以用作左值,并为其赋值,这就是我们在这里所做的。
答案 3 :(得分:0)
这个单行应该有效:
awk 'NR==FNR{a[$1]=substr($0,92,5);next}($1 in a) {$0=substr($0,1,92) a[$1] substr($0,97)}1' file file2