Skip to content

Instantly share code, notes, and snippets.

@akirad
Last active December 17, 2015 22:29
Show Gist options
  • Save akirad/5682691 to your computer and use it in GitHub Desktop.
Save akirad/5682691 to your computer and use it in GitHub Desktop.
A perl script which detect "LF" in files.
use strict;
use warnings;
my $dir = $ARGV[0];
if(! -d $dir){
die "Invalid argument.";
}
$dir =~ s|\\|/|g; # Just in case.
my @fileList;
getFileList($dir, \@fileList);
foreach my $file (@fileList){
if(-f $file){
open(IN, $file);
binmode IN; # If not, all linefeeds are changed to "LF" inside Perl.
my $lineNum=1;
foreach my $line (<IN>){
if($line !~ /\x0d\x0a$/){
#if(!($line =~ /\x0d\x0a$/)){ # This is also OK.
#if($line !~ /\r\n$/){ # This is also OK (on Windows).
print("$file has LF in line $lineNum.\n");
}
$lineNum++;
}
}
}
sub getFileList{
my $dir = shift;
my $ref_fileList = shift; # for output.
opendir(DIR, $dir);
my @fileList = readdir(DIR);
closedir(DIR);
foreach my $file (sort @fileList){
if($file =~ /^\.{1,2}$/){
next;
}
if( -d "$dir/$file"){
getFileList("$dir/$file", $ref_fileList);
}
else{
push(@$ref_fileList, "$dir/$file");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment