perl数据提取 求一个可实现的程序

letusgo1103 2012-05-24 04:37:04
我的一个文件file1为:
AB1 ss 0 i
AB2 rr 0 d
co1 ss 0 l
另一个文件file2为:
AB1 uni1
cd3 uni2
AB2 uni3
hd6 uni4
cd4 uni5
co1 uni6
我输入file1和file2后,希望输出为将file2第二列加在file1对应那行的最后面:
AB1 ss 0 i uni1
AB2 rr 0 d uni3
co1 ss 0 l uni6
...全文
213 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
2012-06-21
  • 打赏
  • 举报
回复
打个小广告,因为个人可用分不多了。
这里有一个问题,没有任何人回答,所以我不能结贴获得分数。
希望有朋友进去回答下,我下次上线立马结贴给分。
有急用。

帖子地址:
http://topic.csdn.net/u/20120605/12/893e36a7-2023-4b6b-8f5d-fe3fe6615ddf.html

http://topic.csdn.net/u/20120605/12/893e36a7-2023-4b6b-8f5d-fe3fe6615ddf.html

bugs2k 2012-06-20
  • 打赏
  • 举报
回复
貌似最简单方式,shell 下:


# join file1 file2
busybeeCS 2012-06-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
Perl code

use warnings;
use strict;
use IO::File;

sub main
{
my $f1=new IO::File("< $ARGV[0]");
my $f2=new IO::File("< $ARGV[1]");

my $line;
my @a1;
while($line=<$f1>)
……
[/Quote]
有几个问题请教一下:
(1)请问语句next if(!$line);是判断空行的吗
(2)语句push (@a1,[split(/\s+/,$line,4)]);为什么不能写成push (@a1,split(/\s+/,$line,4));
(3)my @a2=split(/\s+/,$line,2);
$h2{shift(@a2)}=\@a2;
为什么不能合并成一句$h2{shift(@a2)}=split(/\s+/,$line,2);
这几个问题一直没弄清楚,希望能指点一下,谢了!
国号汗 2012-06-12
  • 打赏
  • 举报
回复

#!c:\Perl\bin\perl
use warnings;
use strict;

my($file1, $file2, $file3) = @ARGV;
my ($line, $key, %hash1, %hash2);
open(FILE1, $file1) || die $!;
while($line = <FILE1>) {
chomp $line;
$line =~ /^(\S+)/;
$hash1{$1} = $line;
}
close FILE1;

open(FILE2, $file2) || die $!;
while($line = <FILE2>) {
chomp $line;
$line =~ /^(\S+)\s(\S+)/;
$hash2{$1}{"Value"} = $2;
$hash2{$1}{"Flag"} = 'True';
}
close FILE2;
open(WRT, ">$file3") || die $!;
#select WRT;
foreach $key (keys(%hash1)) {
if(exists($hash2{$key})) {
print "$hash1{$key}\t$hash2{$key}{'Value'}\n";
$hash2{$key}{'Flag'} = 'Flase';
}
else{
print "$hash1{$key}\n";
}
}

foreach $key (keys(%hash2)) {
if($hash2{$key}{'Flag'} eq 'True') {
print "$key\t$hash2{$key}{'Value'}\n";
}
}
close WRT;
国号汗 2012-06-12
  • 打赏
  • 举报
回复
哈希吧,第一列做key,两个hash表,用其中一个查另一个吧,如果是并集的话 ,就可以类似这样 ${$key}{"Value"}保存后面的值,${$key}{"Flag"}来保存是否有匹配的信息吧
小懿大侠 2012-06-10
  • 打赏
  • 举报
回复
hash怎么做?
proorck6 2012-06-09
  • 打赏
  • 举报
回复
哈希表
cocoa1227 2012-06-07
  • 打赏
  • 举报
回复
用hash是最方便的
cocoa1227 2012-06-07
  • 打赏
  • 举报
回复
用hash,关联数组
letusgo1103 2012-05-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

怎么运行的?文件能访问到吗?
[/Quote]

可以了,谢谢,本人perl实在是很菜,见谅~
fibbery 2012-05-24
  • 打赏
  • 举报
回复
怎么运行的?文件能访问到吗?
letusgo1103 2012-05-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

才学perl,办法较笨

Perl code
#!/usr/bin/env perl

use strict;
use warnings;

open FD1, "file1" or die "Can not open file1\n";
open FD2, "file2" or die "Can not open file2\n";

while (my $a = <FD1>) {
……
[/Quote]

这个我运行了下,然后出现:

CL261Contig2 Unigene14988
Use of uninitialized value in concatenation (.) or string at tq.pl line 11, <FD2> line 597218.
Use of uninitialized value in concatenation (.) or string at tq.pl line 11, <FD2> line 597218.
fibbery 2012-05-24
  • 打赏
  • 举报
回复
命令行啊!
letusgo1103 2012-05-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
use warnings;
use strict;
use IO::File;

sub main
{
my $f1=new IO::File("< $ARGV[0]");
my $f2=new IO::File("< $ARGV[1]");

my $line;
my @a1;
while($line=<$f1>)
{
chomp($line);
next if(!$……
[/Quote]
这个我应该在哪里输入文件名?为什么我运行了没任何输出 啊?
fibbery 2012-05-24
  • 打赏
  • 举报
回复

use warnings;
use strict;
use IO::File;

sub main
{
my $f1=new IO::File("< $ARGV[0]");
my $f2=new IO::File("< $ARGV[1]");

my $line;
my @a1;
while($line=<$f1>)
{
chomp($line);
next if(!$line);
push (@a1,[split(/\s+/,$line,4)]);
}

my %h2;
while($line=<$f2>)
{
chomp($line);
next if(!$line);
my @a2=split(/\s+/,$line,2);
$h2{shift(@a2)}=\@a2;
}

foreach my $item (@a1)
{
print("@$item $h2{$item->[0]}->[0]\n");
}

$f1->close();
$f2->close();
}

main(@ARGV);

AB1 ss 0 i uni1
AB2 rr 0 d uni3
co1 ss 0 l uni6
fibbery 2012-05-24
  • 打赏
  • 举报
回复
use warnings;
use strict;
use IO::File;

sub main
{
my $f1=new IO::File("< $ARGV[0]");
my $f2=new IO::File("< $ARGV[1]");

my $line;
my @a1;
while($line=<$f1>)
{
chomp($line);
next if(!$line);
push (@a1,[split(/\s+/,$line,4)]);
}

my %h2;
while($line=<$f2>)
{
chomp($line);
next if(!$line);
my @a2=split(/\s+/,$line,2);
$h2{shift(@a2)}=\@a2;
}

foreach my $item (@a1)
{
print("@$item $h2{$item->[0]}->[0]\n");
}

$f1->close();
$f2->close();
}

main(@ARGV);

AB1 ss 0 i uni1
AB2 rr 0 d uni3
co1 ss 0 l uni6
bugs2k 2012-05-24
  • 打赏
  • 举报
回复
才学perl,办法较笨

#!/usr/bin/env perl

use strict;
use warnings;

open FD1, "file1" or die "Can not open file1\n";
open FD2, "file2" or die "Can not open file2\n";

while (my $a = <FD1>) {
my($a, $b, $c, $d) = split ' ', $a;
while (my $x = <FD2>) {
my($x, $y) = split ' ', $x;
print "$a $b $c $d $y\n" if $a eq $x;
}
seek FD2, 0, 0;
}
close FD2;
close FD1;

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧