-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace_all_spaces_parens_in_column_titles.pl
72 lines (52 loc) · 1.64 KB
/
replace_all_spaces_parens_in_column_titles.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
#!/usr/bin/env perl
# Replaces all spaces and parentheses in header line with provided replacement value, or
# underscore by default.
# Usage:
# perl replace_all_spaces_parens_in_column_titles.pl [table]
# [optional value to replace spaces with in header line]
# Prints to console. To print to file, use
# perl replace_all_spaces_parens_in_column_titles.pl [table]
# [optional value to replace spaces with in header line] > [output table path]
use strict;
use warnings;
my $table = $ARGV[0];
my $replacement_value = $ARGV[1]; # optional value to replace all spaces with in header line
my $DEFAULT_REPLACEMENT_VALUE = "_";
my $NEWLINE = "\n";
# 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;
}
# sets replacement value
if(!defined $replacement_value or !length $replacement_value)
{
$replacement_value = $DEFAULT_REPLACEMENT_VALUE;
}
# reads in input table
# prints same input table with spaces replaced in header line
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
{
if($first_line) # column titles
{
# replaces all spaces with underscores
$line =~ s/ /$replacement_value/g;
# replaces all parentheses with underscores
$line =~ s/\(/$replacement_value/g;
$line =~ s/\)/$replacement_value/g;
$first_line = 0; # next line is not column titles
}
# prints line
print $line.$NEWLINE;
}
}
close TABLE;
# August 23, 2021