Tab demimited Output/ Arbitary Output column delimiter using MySQL
Selecting Tabular or Tab-Delimited Query Output Format
1.22.1 Problem
mysql produces tabular output when you want tab-delimited output, or vice versa.
1.22.2 Solution
Select the desired format explicitly with the appropriate command-line option.
1.22.3 Discussion
When you use mysql non-interactively (such as to read queries from a file or to send results into a pipe), it writes output in tab-delimited format by default. Sometimes it's desirable to produce tabular output instead. For example, if you want to print or mail query results, tab-delimited output doesn't look very nice. Use the -t (or --table) option to produce tabular output that is more readable:
% mysql -t cookbook < inputfile | lpr % mysql -t cookbook < inputfile | mail paul
The inverse operation is to produce batch (tab-delimited) output in interactive mode. To do this, use -B or --batch.
1.23 Specifying Arbitrary Output Column Delimiters
1.23.1 Problem
You want mysql to produce query output using a delimiter other than tab.
1.23.2 Solution
Postprocess mysql's output.
1.23.3 Discussion
In non-interactive mode, mysql separates output columns with tabs and there is no option for specifying the output delimiter. Under some circumstances, it may be desirable to produce output that uses a different delimiter. Suppose you want to create an output file for use by a program that expects values to be separated by colon characters (:) rather than tabs. Under Unix, you can convert tabs to arbitrary delimiters by using utilities such as tr and sed. For example, to change tabs to colons, any of the following commands would work (TAB indicates where you type a tab character).
The syntax for some versions of tr may be different; consult your local documentation. Also, some shells use the tab character for special purposes such as filename completion. For such shells, type a literal tab into the command by preceding it with Ctrl-V.
% mysql cookbook < inputfile | sed -e "s/ TAB /:/g" > outputfile % mysql cookbook < inputfile | tr " TAB " ":" > outputfile % mysql cookbook < inputfile | tr "\011" ":" > outputfile
sed is more powerful than tr because it understands regular expressions and allows multiple substitutions. This is useful when you want to produce output in something like comma-separated values (CSV) format, which requires three substitutions:
- Escape any quote characters that appear in the data by doubling them so that when you use the resulting CSV file, they won't be taken as column delimiters.
- Change the tabs to commas.
- Surround column values with quotes.
sed allows all three subsitutions to be performed in a single command:
% mysql cookbook < inputfile \ | sed -e 's/"/""/g' -e 's/ TAB /","/g' -e 's/^/"/' -e 's/$/"/' > outputfile
That's fairly cryptic, to say the least. You can achieve the same result with other languages that may be easier to read. Here's a short Perl script that does the same thing as the sed command (it converts tab-delimited input to CSV output), and includes comments to document how it works:
#! /usr/bin/perl -w while (<>) # read next input line { s/"/""/g; # double any quotes within column values s/\t/","/g; # put `","' between column values s/^/"/; # add `"' before the first value s/$/"/; # add `"' after the last value print; # print the result } exit (0);
If you name the script csv.pl, you can use it like this:
% mysql cookbook < inputfile | csv.pl > outputfile
If you run the command under a version of Windows that doesn't know how to associate .pl files with Perl, it may be necessary to invoke Perl explicitly:
C:\> mysql cookbook < inputfile | perl csv.pl > outputfile
Perl may be more suitable if you need a cross-platform solution, because it runs under both Unix and Windows. tr and sed normally are unavailable under Windows.
0 comments:
Post a Comment