-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_column_after_query.pl
116 lines (94 loc) · 2.43 KB
/
split_column_after_query.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
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env perl
# Splits column into two columns following appearance of query in each column value. If
# a cell does not contain the query, duplicates the column value.
# Usage:
# perl split_column_after_query.pl [table] "[column name]" "[query]"
# Prints to console. To print to file, use
# perl split_column_after_query.pl [table] "[column name]" "[query]" > [output table path]
use strict;
use warnings;
my $table = $ARGV[0];
my $title_of_column_to_split = $ARGV[1];
my $query = $ARGV[2];
my $NEWLINE = "\n";
my $DELIMITER = "\t";
# if 1, input column title is actually column number
my $INPUT_COLUMN_NUMBER_NOT_TITLE = 0;
# verifies that input file exists and is not empty
if(!$table or !-e $table or -z $table)
{
print STDERR "Error: table not provided, does not exist, or empty:\n\t"
.$table."\nExiting.\n";
die;
}
# reads in and processes input table
my $column_to_split = -1;
my $first_line = 1;
open TABLE, "<$table" || die "Could not open $table to read; terminating =(\n";
while(<TABLE>) # for each row in the file
{
chomp;
my $line = $_;
if($line =~ /\S/) # if row not empty
{
my @items_in_line = split($DELIMITER, $line, -1);
if($INPUT_COLUMN_NUMBER_NOT_TITLE)
{
$column_to_split = $title_of_column_to_split;
}
elsif($first_line) # column titles
{
# identifies columns to include
my $column = 0;
foreach my $column_title(@items_in_line)
{
if(defined $column_title and $column_title eq $title_of_column_to_split)
{
$column_to_split = $column;
}
$column++;
}
# verifies that we have found all columns to include
if($column_to_split == -1)
{
print STDERR "Error: expected column title ".$title_of_column_to_split
." not found in table ".$table."\nExiting.\n";
die;
}
$first_line = 0; # next line is not column titles
}
# prints all column values
my $column = 0;
my $printing_first_column = 1;
foreach my $value(@items_in_line)
{
# splits column value if we are in the column to split
if($column == $column_to_split)
{
if($value =~ /^([^$query]*)$query(.*)$/)
{
$value = $1.$DELIMITER.$2;
}
else
{
$value = $value.$DELIMITER.$value;
}
}
# prints delimiter
if(!$printing_first_column)
{
print $DELIMITER;
}
$printing_first_column = 0;
# prints value
if(defined $value)
{
print $value;
}
$column++;
}
print $NEWLINE;
}
}
close TABLE;
# December 6, 2022