-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseTable.pl
executable file
·105 lines (90 loc) · 4.06 KB
/
parseTable.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/perl
#
##########################################################################################
#
# Version : v1.2
#
# Last update : 2018-03-22
# Updated by : Sander W. van der Laan | [email protected].
# Originally written by : Paul I.W. de Bakker | UMC Utrecht, [email protected].
#
# Given a space/tab delimited file as input, this script will collect the columns as
# indicated by the comma-separated list after the '--col'-flag, and return these.
#
# Usage: cat [table.txt] | parse.pl --col COL1[,COL2,COL3...]
#
##########################################################################################
use strict;
use Getopt::Long;
my @Columns = ();
my $Sep = " ";
my $noheader = '';
GetOptions(
"col=s" => \@Columns,
"sep=s" => \$Sep,
"no-header" => \$noheader
);
@Columns = split(/,/, join(',',@Columns));
if ( $#Columns < 0 ) {
print "usage: %>parseTable.pl --col COL1[,COL2,COL3,...] \n";
print "Prints specified columns from STDIN\n";
exit();
}
my @column_index = ();
for ( my $i=0; $i <= $#Columns; $i++ ) {
$column_index[$i] = -1;
# print "$Columns[$i] \n";
}
my $linecount = 0;
while(my $c = <STDIN>) {
$c=~s/\s+$//;
$c=~s/^\s+//;
my @fields = split /\s+/, $c;
if ( $linecount == 0 ) {
for (my $i=0; $i<=$#fields; $i++) {
for (my $j=0; $j<=$#Columns; $j++) {
if ( $fields[$i] eq $Columns[$j] ) {
$column_index[$j] = $i;
next;
}
}
}
for (my $j=0; $j<=$#Columns; $j++) {
if ( $column_index[$j] == -1 ) { die "*** ERROR *** $Columns[$j] is not recognized. Please double back.\n"; }
if ( ! $noheader ) {
print STDOUT $j>0 ? $Sep : "", $Columns[$j];
}
}
if ( ! $noheader ) { print STDOUT "\n"; }
}
else {
for (my $i=0; $i<=$#Columns; $i++) {
print STDOUT $i>0 ? $Sep : "", $fields[$column_index[$i]];
}
print STDOUT "\n";
}
$linecount++;
}
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# + The MIT License (MIT) +
# + Copyright (c) 2016 Sander W. van der Laan +
# + +
# + Permission is hereby granted, free of charge, to any person obtaining a copy of this +
# + software and associated documentation files (the \"Software\"), to deal in the +
# + Software without restriction, including without limitation the rights to use, copy, +
# + modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, +
# + and to permit persons to whom the Software is furnished to do so, subject to the +
# + following conditions: +
# + +
# + The above copyright notice and this permission notice shall be included in all copies +
# + or substantial portions of the Software. +
# + +
# + THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +
# + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +
# + PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +
# + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF +
# + CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE +
# + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +
# + +
# + Reference: http://opensource.org. +
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++