|
Replacing each <TAB> character in a file with
a comma character can be accomplished several different
ways with Perl. (I guess that's one reason the Perl slogan
says There's more than one way to do it.)
My favorite way is like this, from the command-line:
perl -pi.bak -e 's/\t/,/g' myfile.txt
From the Unix (or DOS) command-line, you can invoke Perl
just like this to convert the file named myfile.txt.
This command edits the file in-place, and makes a
backup file named myfile.txt.bak (in case we make an
error). The new version of myfile.txt contains
the changes you just made -- all the <TAB> characters
have been converted to comma's.
The command line options I used here are:
- -p
- Assumes an input loop around your script.
- -i EXT
- Files processed by the <> construct are
to be edited in-place.
- -e COMMANDS
- May be used to enter one line of a script (or, in this
case, a one-line script).
Comments
I just ran into a situation where I needed to
convert a tab-delimited spreadsheet file into a comma-delimited
file I could import into a database, and used this exact
command.
Note that this is an improvement over the Unix sed
command, where you can't edit a file in-place. With
sed, you have to open the original file, read
it's contents, write the changes to a new file, move the
original file to a backup name, and then move the new file
to the original file name. This is easier done than said,
but requires the use of a shell wrapper. Using Perl to edit
the file in-place is certainly much easier.
|