Text 基于第三个文件(可能使用awk?)上的顺序键向文件添加列
我有一个以下类型的文本文件: file1.txt:Text 基于第三个文件(可能使用awk?)上的顺序键向文件添加列,text,awk,gawk,text-manipulation,Text,Awk,Gawk,Text Manipulation,我有一个以下类型的文本文件: file1.txt: > -L"200100" -6.37447846851 36.186032575 -6.37383387763 36.1858844144 -6.37377079559 36.1858390355 > -L"200200" -6.31950329044 36.1191615625 -6.31968900314 36.119114902 -6.319089132
> -L"200100"
-6.37447846851 36.186032575
-6.37383387763 36.1858844144
-6.37377079559 36.1858390355
> -L"200200"
-6.31950329044 36.1191615625
-6.31968900314 36.119114902
-6.31908913286 36.1191091689
> -L"200100"
-6.31911178588 36.1188919898
-6.31918479464 36.1188976987
> -L"200250"
-6.31909865128 36.1186432256
-6.31920604922 36.1186522368
-6.31941109375 36.1187126272
....
第二个文本文件如下:
file2.txt
-51
-14
-101
-32
...
我希望每次在file1.txt上找到类型为'>-L“200100'(实际上以'>-L'开头的任何行)的行时,都会附加第三列,并在file2.txt中获取相应的(顺序)值。在我的示例中,输出为:
file3.txt:
> -L"200100"
-6.37447846851 36.186032575 -51
-6.37383387763 36.1858844144 -51
-6.37377079559 36.1858390355 -51
> -L"200200"
-6.31950329044 36.1191615625 -14
-6.31968900314 36.119114902 -14
-6.31908913286 36.1191091689 -14
> -L"200100"
-6.31911178588 36.1188919898 -101
-6.31918479464 36.1188976987 -101
> -L"200250"
-6.31909865128 36.1186432256 -32
-6.31920604922 36.1186522368 -32
-6.31941109375 36.1187126272 -32
....
file1.txt中出现'>-L'的次数等于file2.txt中相同的行数
用awk/gawn单线客机可以吗
谢谢。如果您将其保存为“Append3rdCol”,然后执行以下操作,那么在Perl中可以非常轻松地执行此操作:
chmod +x Append3rdCol
./Append3rdCol
代码如下:
#!/usr/bin/perl
use warnings;
use strict;
my $extra;
open(F1,"file1.txt") or die;
open(F2,"file2.txt") or die;
while(<F1>){
chomp; # Strip line ending
if(m/^>/){ # If line starts with ">"
print "$_\n";
chomp($extra=readline(*F2)); # Read next line of file2 and trim <CR>
} else {
print "$_ $extra\n";
}
}
#/usr/bin/perl
使用警告;
严格使用;
我的$extra;
打开(F1,“file1.txt”)或死亡;
打开(F2,“file2.txt”)或死亡;
while(){
chomp;#带线结束
if(m/^>/){#if行以“>”开头
打印“$\u\n”;
chomp($extra=readline(*F2))#读取文件2的下一行并修剪
}否则{
打印“$\u$extra\n”;
}
}
试试这一行:
awk 'NR==FNR{k[NR]=$0;next}/^>/{++i;print;next}{print $0,k[i]}' f2 f1
根据您的数据,上列给出:
kent$ awk 'NR==FNR{k[NR]=$0;next}/^>/{++i;print;next}{print $0,k[i]}' f2 f1
> -L"200100"
-6.37447846851 36.186032575 -51
-6.37383387763 36.1858844144 -51
-6.37377079559 36.1858390355 -51
> -L"200200"
-6.31950329044 36.1191615625 -14
-6.31968900314 36.119114902 -14
-6.31908913286 36.1191091689 -14
> -L"200100"
-6.31911178588 36.1188919898 -101
-6.31918479464 36.1188976987 -101
> -L"200250"
-6.31909865128 36.1186432256 -32
-6.31920604922 36.1186522368 -32
-6.31941109375 36.1187126272 -32