-
Notifications
You must be signed in to change notification settings - Fork 0
/
LE2Unix.pl
executable file
·93 lines (83 loc) · 2.32 KB
/
LE2Unix.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
#!/usr/bin/perl
=begin
Takes a list of paths and genome names as th input (same as kSNP3)
checks each file and returns the kind of line endings it has
Usage LE2Unix inputList
=cut
use warnings;
use strict;
my $listFile = $ARGV[0];
my $genomeID = '';
my $filePath = ''; #path to the file corresponding to the $genomeID
my $line = '';
my @temp = ();
my @Files = (); #col0 is fileID col1 is path
my $LE = '';
my $fileName = '';
my $pathToDirectory = '';
my @args = ();
my $macFlag = 'F';
#get the list of genomes and paths into @Files
open(INFILE, $listFile) or die "Can't open $listFile for reading. $!";
while ($line = <INFILE>) {
chomp $line;
@temp = split /\t/, $line;
$genomeID = $temp[1];
$filePath = $temp[0];
push @Files, [$genomeID, $filePath];
}
close INFILE;
#test each file to determine its file endings
for (my $i = 0; $i <scalar @Files; $i++) {# scalar @Files
$LE = getLE($Files[$i][1]);
@temp = split /\//, $Files[$i][1];
$fileName = pop @temp;
$pathToDirectory = join "\/", @temp;
if ($LE ne 'Unix'){
print "The line endings of $fileName are $LE\n";
}
if ($LE eq 'Windows') {
open(INFILE, $Files[$i][1]) or die "Can't open $Files[$i][1] for reading. $!";
open (OUTFILE,'>temp.txt') or die "Can't open temp.txt for writing. $!";
while ($line = <INFILE>) {
$line =~ s/\r\n/\n/;
print OUTFILE "$line";
}
close INFILE;
close OUTFILE;
rename ('temp.txt', $fileName);
@args = ('mv', "$fileName", "$pathToDirectory");
system(@args) == 0 or die "system @args failed: $!";
}
elsif($LE eq 'Mac') {
print "\nWARNING!!! $fileName has Classic Mac line endings.\nYou should manually change the line endings to Unix!\n";
$macFlag = 'T';
}
}
if ($macFlag eq 'T') {
print "\n\nThe files listed above have classic Mac line endings.\nYou should manually change those endings to Unix.\n\n";
die;
}
sub getLE
{
my $file = $_[0];
my $line = '';
my $lastChar = '';
my $secondLastChar = '';
my $LE = ''; #Windows, Unix or Mac
open (INFILE, $file) or die "Can't open $file for reading. $!";
$line = <INFILE>;
$lastChar = ord (substr($line, -1, 1));
$secondLastChar = ord(substr($line, -2, 1));
if($lastChar == 10 and $secondLastChar == 13) {
$LE = 'Windows';
}
elsif ($lastChar == 10 and $secondLastChar != 13) {
$LE = 'Unix';
}
elsif ($lastChar == 13) {
$LE = 'Mac';
}
close INFILE;
return $LE;
}