Perl regex search/replace FAQ: How do I replace every TAB character in a file with a comma? (More generally, how do I search for a Perl regex pattern, and replace it with a different string?)
Perl regex search and replace
Replacing each TAB character in a file with a comma character can be accomplished several different ways with Perl. The key in any approach is to perform a Perl regex search and replace operation.
(Note: Please make a backup of your file(s) before continuing.)
My favorite way to solve this problem is to run a Perl search and replace command from the Linux command line. I like to do this using a command which edits the file in place, so I don't have to do that pesky process of making the changes, saving them to a different file, and then having to rename that new file to the old filename.
So, to run a Perl search and replace command from the command line, and edit the file in place, use this command:
perl -pi.bak -e 's/\t/,/g' myfile.txt
This command makes the search-and-replace changes to the file 'myfile.txt', and in this case I also make 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 commas.
Perl command line options used
The command line options I used in this Perl example 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).
Perl regex search and replace - More general situation
Of course the more general question is "How do I replace every instance of Character A with Character B in a file with Perl?" Just replace the TAB and comma in the Perl code/command I shared above, and you should be in good shape.
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.