in reply to Re: Re: Opening a file for reading or writing (was: Newbie)
in thread Opening a file for reading or writing (was: Newbie)

Try this: perl -i.bak -pe "s/\'/\t/;" somefile.dat

The entire problem can be solved with a one-liner. Using tr or y instead of s might be good in this case because s loses performance due to regex computation. You don't really need a regex here -- you're only replacing one character -- unless, of course, you only want to replace the first one, in which case use s.

Modifying a file, then putting the modified file in place of the original file, is known as modifying the file "in place", which is done using the i switch. The .bak is to make a backup as somefile.dat.bak, so that in case of error, you can recover the original data. Since you are modifying it in place, your while(<FILE>) { ... } loop becomes a while(<>) { ... } loop. To get the modified data to be output back to the file, you need to use a print statement, thus: while(<>) { ... print; } This can be accomplished by using the p switch, which places that around whatever code is given. Finally, the resulting code is so short that it can be one-lined using e, which is "run code from next argument".

See perlman:perlrun and perlman:perlop for more information on perl options and operators.

Drake Wilson

Update: D'oh! Corion had already pointed out -i. Oh well.