-
Notifications
You must be signed in to change notification settings - Fork 2
/
addTail.pl
executable file
·49 lines (46 loc) · 1.3 KB
/
addTail.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
#!/bin/env perl
use strict;
use 5.010;
use strict;
use File::Basename;
use Getopt::Long;
use lib dirname $0;
use pm::common;
my ($delimiter, $columnN, $help) = ("\t");
my $opt=GetOptions(
'd|delimiter=s' => \$delimiter,
'c|column=s' => \$columnN,
'h|help' => sub{usage()}
) || usage();
$ARGV[0]='-' if !defined $ARGV[0];
open IN, $ARGV[0] or die "Can't open $ARGV[0]: $!";
my $maxColumnNO = 0;
if(!defined $columnN){
$maxColumnNO = &common::getColumnNumber(\*IN);
close IN;
open IN,$ARGV[0];
}else{
$maxColumnNO = $columnN;
}
while(<IN>){
chomp;
my @fields=split "$delimiter";
if (@fields < $maxColumnNO){
for(my $i = @fields; $i < $maxColumnNO; $i++){
push @fields, '';
}
}
say join "$delimiter", @fields;
}
sub usage{
my $scriptName = basename $0;
print <<HELP;
Usage: perl $scriptName INPUT.tsv >OUTPUT.tsv
if INPUT.tsv isn't specified, input from STDIN
output to STDOUT
-d --delimiter STRING Delimter to split line[tab]
-c --column INT Column number upto which line is appended. Force when INPUT from STDIN
-h --help Print this help information
HELP
exit(-1);
}