in reply to Another dos to unix question

perl -i -pe 's/\cM*$//g' file file ...

Replies are listed 'Best First'.
Re: Re: Another dos to unix question
by Anonymous Monk on May 18, 2004 at 15:25 UTC
    i am aware of the single command line stuff. But would prefer it in a script form for later use etc, thanks
      sub dos2unix { system( qq{perl -i -pe 's/\\cM*//g' $_} ) for @_ }

      (No more inefficient than opening a pipe (?!?!) to mv rather than using unlink())

        Well, it's quite inefficient. You're starting a process for each file, and do a substitution for every empty string.
        sub dos2unix { system perl => '-i', '-pe' => 'tr/\x0D//d' @_ }

        Abigail

      update: sorry -- I missed this part in your OP: "how i can convert a list of files, stored in a 'list file'" I have added a bit to the script to account for this. As modified, you can pipe a list of file names to the script on stdin (e.g. "ls | script.pl") or store the list(s) in some named file(s) and give the list file name(s) to the script as ARGV (e.g. "script.pl list [list2 ...]" /update.

      Well then, you could create a perl script file as follows:

      #!/usr/bin/perl # in-place dos-to-unix conversion (remove ^M's): my @file_list = <>; # update: adding these two lines to chomp @file_list; # read a list of file names for my $file ( @file_list ) { # not "@ARGV" open( I, $file ) or die "$file: $!"; open( O, ">$file.unx" ) or die "$file.unx: $!"; while (<I>) { s/\cM*//g; print O; } close I; close O; rename "$file.unx", $file or die "renaming $file.unx to $file: $!" +; }

      Note that this differs slightly from the code in your original post: your strange and less efficient approach would cause each "CRLF" sequence in a dos file to be converted to "LFLF", which means your output files would have twice as many "lines" as the originals, and half of those output lines would be empty. The version here simply removes the "CR". (Or maybe it was your intention that dos files should show up in unix as "double-spaced"?)