0

I have two different script to change DOS2UNIX and MAC2UNIX endline format

DOS2UNIX: perl -pi -e 's/\r\n/\n/;' < dos1.txt > dos2.txt

MAC2UNIX: perl -p -e 's/\r/\n/g' < mac1.txt > mac2.txt

Is there anyway get these functionalities in a signle command to change EOL UNIX format!

Thanks

Charles
  • 50,943
  • 13
  • 104
  • 142
Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84
  • what's wrong with the `dos2unix` and `mac2unix` commands? Why reinvent the wheel? – codaddict May 09 '12 at 07:05
  • not available and does have any previllage to install them – Mohamed Saligh May 09 '12 at 07:07
  • what does recursive (in your title) have to do with it? As an alternate solution, make aliases stored in the appropriate `.profile or .bash_rc or ...?` file, like `alias DOS2UNIX="perl -pi -e 's/\r\n/\n/;'"` and then supply your inputfile and redirection after it? Or are you saying you can't install perl on your machine either? Good luck. – shellter May 09 '12 at 14:33
  • The `-i` option is not useful with redirected input. If specifies in-place editing of the files named on the command line. – tripleee May 09 '12 at 16:33
  • 1
    If you don't have permission to install them, how do you have permission to install a Perl program? ;) – brian d foy May 10 '12 at 03:14

1 Answers1

2

This handles both cases.

perl -pe 's/\r\n?/\n/g'

It (semi-obviously) does the wrong thing if you have a DOS file with a bare CR somewhere within a line, which should (perhaps) not be converted to LF.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks! I have already got the answer `perl -pe 's/\r?\n|\r/\n/g'` – Mohamed Saligh May 10 '12 at 07:32
  • 1
    I bet mine is faster, though, as it will only try to replace when there is something which needs to be replaced. (Yours will do a number of no-op `\n` -> `\n` substitutions if you run it on a file which is already in Unix format.) – tripleee May 10 '12 at 07:58