in reply to Can anyone figure how this works?
Presumably you call this script like this:
$ fix.pl wrong.data fixed.data
Here is a blow by blow:
# command line arguments are available to the script in the # @ARGV array. Thus the first argument is in $ARGV[0], the # second in $ARGV[1].... # Open the file specified in the first command line arg for reading open(IN, "$ARGV[0]") || die "unable to open $ARGV[0]"; # Open the file specified in the second command line arg for writing open(OUT, ">$ARGV[1]") || die "unable to open $ARGV[0]"; # stop perl making automatic \r\n => \n or \r => \n line ending # conversions which are required on Win32 and Mac respectively binmode(OUT); #set output mode as binary # now iterate over our input file on line at a time while(<IN>) { # if we have a line that contains only "\n" - ie a blank line if(/^\n$/) { # then we print "\r\n" instead of the existing "\n" into our o +utput file print OUT "\r\n"; } # otherwise just print out the totally unaltered line else { print OUT; } # else just print line with a CR } # close the input and output files close(IN); close(OUT);
If you want a short way to do the same this will do it with an inplace edit. You call it like this fix.pl data The data in data will get munged and a backup will be made called data.bak The backup will contain the original data, the argument file the modified data.
#!/usr/bin/perl -i.bak -w while (<>) { s/^\n$/\r\n/; print; }
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Can anyone figure how this works?
by demerphq (Chancellor) on Sep 30, 2001 at 05:38 UTC | |
by tachyon (Chancellor) on Sep 30, 2001 at 22:10 UTC | |
by thesundayman (Novice) on Oct 03, 2001 at 14:57 UTC | |
by tachyon (Chancellor) on Oct 03, 2001 at 18:49 UTC |