-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabsToSpaces.pl
More file actions
82 lines (64 loc) · 2.27 KB
/
Copy pathtabsToSpaces.pl
File metadata and controls
82 lines (64 loc) · 2.27 KB
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
#Author: Eric Spaulding
use strict; #enforce strict mode
use warnings; #give warnings
$| = 1; ##dumps print lines as you go rather than saving up the buffer
binmode STDOUT, ":utf8"; ##alows for the use of some unicode characters
use constant { true => 1, false => 0 }; #define true and false for the rest of the script
my $os = $^O; #get the current operating system that the script is being run under
#os results that I've seen
#windows8 -> MSWin32
#linux mint14 -> linux
#juno -> linux
#cygwin use bash from cmd -> MSWin32 (in other words it reports the native windows os)
#cygwin terminal -> cygwin
#print "running in: $os\n";
#exit;
main();
#wrap all the main code in a function in order to keep the global namespace clean
sub main{
my $spacesPerTab = 4; #default is to replace tabs with 4 spaces
my $file = ""; #default is no file. script must abort in this situation.
my $scriptname = $0; #the name of this script
my $total = $#ARGV + 1;
#TODO: look into a rigourous and correct way to process commandline arguments
if ($total == 1 || $total == 2){
$file = $ARGV[0];
}
if ($total == 2){
$spacesPerTab = $ARGV[1];
}
if(length($file) == 0){
print "\nno target file specified\n";
exit;
}
writefile({ filename => $file,
lines => readfile({filename => $file}),
spt => $spacesPerTab});
}
#read a file and put the query sequences into memory
sub readfile{
my $args = shift;
my $filename = $args->{'filename'};
#Open the file
#print "opening data from $args->{'filename'}\n";
open(IN,$filename) || die "\n$filename not found\n";
my @lines = <IN>; #read in all of the lines at once, so the lines are stored in an array
close(IN); #close the file
return \@lines;
}
sub writefile{
my $args = shift;
my $filename = $args->{'filename'};
my $lines = $args->{'lines'};
my $spt = $args->{'spt'};
my $spaces = (" ") x $spt;
#my $test = ("x") x $spt;
#print "\n$test$spaces$test\n";
#open the file for writing
open(OUT,">$filename") || die "\ncouldn't open $filename for writing\n";
foreach my $line (@$lines){
$line =~ s/\t/$spaces/g;
print OUT $line;
}
close(OUT);
}