andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:
I primarily work in a linux server environment. I often encounter files that were originally written on another operating system. That can make life hard for scripts like the following:
I'd like my scripts to be platform-agnotic, that is, process files regardless of being created on a Unix, Windows or Mac platform.$ echo -e "123\n456\n789" > foo.txt $ hexdump -C foo.txt 00000000 31 32 33 0a 34 35 36 0a 37 38 39 0a |123.456.7 +89.| 0000000c $ cat foo.txt | perl -wle 'use strict; while(my $line = <>) { chomp $l +ine; ($line =~ m/^\d+$/) ? print qq{number ok:"$line"} : print qq{num +ber error:"$line"} }' number ok:"123" number ok:"456" number ok:"789" $ unix2dos foo.txt unix2dos: converting file foo.txt to DOS format ... $ hexdump -C foo.txt 00000000 31 32 33 0d 0a 34 35 36 0d 0a 37 38 39 0d 0a |123..456. +.789..| 0000000f $ cat foo.txt | perl -wle 'use strict; while(my $line = <>) { chomp $l +ine; ($line =~ m/^\d+$/) ? print qq{number ok:"$line"} : print qq{num +ber error:"$line"} }' "umber error:"123 "umber error:"456 "umber error:"789 $
I use unix2dos manually on occations, but I'd rather have my perl scripts do this themselves when they encounter files created on a foreign operating system. Is there a reliable way to do this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Process file regardless of the platform it was created on
by moritz (Cardinal) on May 20, 2008 at 07:57 UTC | |
|
Re: Process file regardless of the platform it was created on
by psini (Deacon) on May 20, 2008 at 08:02 UTC | |
by andreas1234567 (Vicar) on May 20, 2008 at 08:25 UTC | |
by almut (Canon) on May 20, 2008 at 08:36 UTC | |
by psini (Deacon) on May 20, 2008 at 08:32 UTC | |
|
Re: Process file regardless of the platform it was created on
by graff (Chancellor) on May 21, 2008 at 01:49 UTC |