-
Notifications
You must be signed in to change notification settings - Fork 2
/
lineCollapse.pl
executable file
·66 lines (58 loc) · 1.66 KB
/
lineCollapse.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env perl
=hey
Author: Shijian Sky Zhang
E-mail: [email protected]
=cut
use 5.012;
use warnings;
use Getopt::Long;
use File::Basename;
my ($field, $separater, $split) = (1, ',');
sub usage{
my $scriptName = basename $0;
print <<HELP;
Usage: perl $scriptName INPUT.tsv >OUTPUT.tsv
If INPUT.tsv isn't specified, input from STDIN
Option:
-f --field INT Field to operate
-s --separater STR Separate for collapse/split[$separater]
--split Split one line to multiple lines
-h --help Print this help information
HELP
exit(-1);
}
GetOptions(
'f|field=i' => \$field,
's|separater=s' => \$separater,
'split' => \$split,
'h|help' => sub{usage()}
) || usage();
$ARGV[0] = '-' unless defined $ARGV[0];
open IN, "$ARGV[0]" or die "Can't read file ($ARGV[0]): $!";
if(defined $split){
while(<IN>){
my @fields = split "\t";
my @subFields = split $separater, $fields[$field];
for my $subField(@subFields){
$fields[$field] = $subField;
print join "\t", @fields;
}
}
}else{
my %hash;
while(<IN>){
$_ .= "\n" if $_ !~ /\n$/;
my @fields = split "\t";
my $subField = $fields[$field];
splice @fields, $field, 1;
my $otherFields = join "\t", @fields;
push @{$hash{"$otherFields"}}, $subField;
}
for my $key(keys %hash){
my @subFields = @{$hash{$key}};
my $joinedValue = join $separater, @subFields;
my @fields = split "\t", $key;
splice @fields, $field, 0, $joinedValue;
print join "\t", @fields;
}
}