- 论坛徽章:
- 0
|
本帖最后由 ba_du_co 于 2015-05-27 15:57 编辑
大体是这种感觉
测试样例:: 文件 TEST.data
- I id 1001 1002 1003
- M id0101 A G T
- N id0102 G T c
- O id0103 A G T
- P id0104 G T c
- Q id0105 A G T
- R id0106 G T c
- S id0107 A G T
- T id0108 G T c
复制代码 提取字符 3 个::
输出大约是这种感觉::
提取字符 大体有 3 个 左右
文件 TEST.data 大体有 9 行 左右
文件 TEST.data 大体有 5 列 左右
开始提取字符::
随机字符 < T > 具体位置大体在:: 行 3, 列 4 [ N id0102, id: 1002 ]
随机字符 < T > 具体位置大体在:: 行 6, 列 5 [ Q id0105, id: 1003 ]
随机字符 < G > 具体位置大体在:: 行 8, 列 4 [ S id0107, id: 1002 ]
大体是这种感觉
测试:: perl 代码.pl
输出到新文件:: perl 代码.pl > 新文件
大约是这种感觉的代码::- #!/usr/bin/perl
- use 5.020;
- my $file = 'TEST.data';
- my $picks = 3; # 随机提取 XXX 个字符
- my $last_line = 1;
- my %line_numbers;
- open my ($handle), $file or die "$!: [ $file ] >>";
- die "DIE:: 这个操作系统大约不是 linux!\n" if $^O ne 'linux';
- my ($lines) = split /\s+/, `wc -l $file`;
- my ( undef, undef, @id ) = split /\s+/, <$handle>;
- my $cols = @id;
- $lines -= 1;
- while ( keys %line_numbers < $picks ) {
- my $this_line = int rand $lines;
- next if exists $line_numbers{$this_line};
- $line_numbers{$this_line} = undef;
- $last_line = $this_line if $last_line < $this_line;
- }
- $last_line += 2;
- %line_numbers = map { $_ + 2 => undef } keys %line_numbers;
- say STDERR "提取字符 大体有 $picks 个 左右",;
- say STDERR "文件 $file 大体有 ", 1 + $lines, ' 行 左右';
- say STDERR "文件 $file 大体有 ", 2 + $cols, ' 列 左右';
- say STDERR "\n开始提取字符::\n";
- while (<$handle>) {
- next unless exists $line_numbers{$.};
- my ( $first, $identification, @characters ) = split;
- my $irc = int rand $cols;
- print "随机字符 < $characters[$irc] > 具体位置大体在:: 行 $., 列 ";
- print $irc + 3, " [ $first $identification, id: $id[$irc] ]\n";
- last if $. == $last_line;
- }
- close $handle;
- say STDERR "\n大体是这种感觉\n";
- __DATA__
- I id 1001 1002 1003
- M id0101 A G T
- N id0102 G T c
- O id0103 A G T
- P id0104 G T c
- Q id0105 A G T
- R id0106 G T c
- S id0107 A G T
- T id0108 G T c
复制代码 |
|