-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_header_line_to_tables.pl
57 lines (41 loc) · 1.2 KB
/
add_header_line_to_tables.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
#!/usr/bin/env perl
# Adds header line to all input files. Saves output tables in new files at same path with
# _with_header_line.txt extension. Four spaces in the input header line are replaced with
# tabs.
# Usage:
# perl add_header_line_to_tables.pl "[header line]" [table] [optional additional table]
# [etc.]
use strict;
use warnings;
my $header_line = $ARGV[0];
my @tables = @ARGV[1..$#ARGV];
my $NEWLINE = "\n";
my $DELIMITER = "\t";
# verifies that inputs are provided
if(!$header_line)
{
print STDERR "Error: header line not provided. Exiting.\n";
die;
}
if(scalar @tables < 1)
{
print STDERR "Error: input tables not provided. Exiting.\n";
die;
}
# replaces spaces with tabs in the header line
$header_line =~ s/ /\t/g;
# reads in and prints each table
foreach my $table(@tables)
{
my $output_table = $table."_with_header_line.txt";
open OUTPUT_TABLE, ">$output_table" || die "Could not open output table to write; terminating =(\n";
print OUTPUT_TABLE $header_line.$NEWLINE;
open TABLE, "<$table" || die "Could not open $table to read; terminating =(\n";
while(<TABLE>) # for each row in the file
{
print OUTPUT_TABLE $_;
}
close TABLE;
close OUTPUT_TABLE;
}
# November 13, 2022