Problem Solution Discussion Specifying Arbitrary Output Column Delimiters

mysql cookbook outputfile However, if you t ry t o run m ysql int eract ively wit h t he out put redirect ed, you wont be able t o see what youre t yping, so generally in t his case youll also t ake query input from a file or anot her program : mysql cookbook inputfile outputfile You can also send query out put t o anot her program . For exam ple, if you want t o m ail query out put t o som eone, you m ight do so like t his: mysql cookbook inputfile | mail paul Not e t hat because m ysql r uns non- int eract ively in t hat cont ext , it produces t ab-delim it ed out put , which t he m ail recipient m ay find m ore difficult t o read t han t abular out put . Recipe 1.22 shows how t o fix t his problem .

1.22 Selecting Tabular or Tab-Delimited Query Output Format

1.22.1 Problem

m ysql produces t abular out put when you want t ab-delim it ed out put , or vice versa.

1.22.2 Solution

Select t he desired form at explicit ly wit h t he appropriat e com m and- line opt ion.

1.22.3 Discussion

When you use m ysql non- int eract ively such as t o read queries from a file or t o send result s int o a pipe , it writ es out put in t ab-delim it ed form at by default . Som et im es it s desirable t o produce t abular out put inst ead. For exam ple, if you want t o print or m ail query result s, t ab- delim it ed out put doesnt look very nice. Use t he - t or - - t able opt ion t o produce t abular out put t hat is m ore readable: mysql -t cookbook inputfile | lpr mysql -t cookbook inputfile | mail paul The inverse operat ion is t o produce bat ch t ab- delim it ed out put in int eract ive m ode. To do t his, use - B or - - bat ch.

1.23 Specifying Arbitrary Output Column Delimiters

1.23.1 Problem

You want m ysql t o produce query out put using a delim it er ot her t han t ab.

1.23.2 Solution

Post process m ysqls out put .

1.23.3 Discussion

I n non- int eract ive m ode, m ysql separat es out put colum ns wit h t abs and t here is no opt ion for specifying t he out put delim it er. Under som e circum st ances, it m ay be desirable t o produce out put t hat uses a different delim it er. Suppose you want t o creat e an out put file for use by a program t hat expect s values t o be separat ed by colon charact ers : rat her t han t abs. Under Unix, you can convert t abs t o arbit rary delim it ers by using ut ilit ies such as t r and sed. For exam ple, t o change t abs t o colons, any of t he following com m ands would work TAB indicat es where you t ype a t ab charact er : [ 7] [ 7] The synt ax for som e versions of t r m ay be different ; consult your local docum ent at ion. Also, som e shells use t he t ab charact er for special purposes such as filenam e com plet ion. For such shells, t ype a lit eral t ab int o t he com m and by preceding it wit h Ct rl- V. mysql cookbook inputfile | sed -e s TAB :g outputfile mysql cookbook inputfile | tr TAB : outputfile mysql cookbook inputfile | tr \011 : outputfile sed is m ore powerful t han t r because it underst ands regular expressions and allows m ult iple subst it ut ions. This is useful when you want t o produce out put in som et hing like com m a- separat ed values CSV form at , which requires t hree subst it ut ions: • Escape any quot e charact ers t hat appear in t he dat a by doubling t hem so t hat when you use t he result ing CSV file, t hey wont be t aken as colum n delim it ers. • Change t he t abs t o com m as. • Surround colum n values wit h quot es. sed allows all t hree subsit ut ions t o be perform ed in a single com m and: mysql cookbook inputfile \ | sed -e sg -e s TAB ,g -e s -e s outputfile That s fairly crypt ic, t o say t he least . You can achieve t he sam e result wit h ot her languages t hat m ay be easier t o read. Heres a short Perl script t hat does t he sam e t hing as t he sed com m and it convert s t ab-delim it ed input t o CSV out put , and includes com m ent s t o docum ent how it works: usrbinperl -w while read next input line { sg; 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; I f you nam e t he script csv.pl, you can use it like t his: mysql cookbook inputfile | csv.pl outputfile I f you run t he com m and under a version of Windows t hat doesnt know how t o associat e .pl files wit h Perl, it m ay be necessary t o invoke Perl explicit ly: C:\ mysql cookbook inputfile | perl csv.pl outputfile Perl m ay be m ore suit able if you need a cross-plat form solut ion, because it runs under bot h Unix and Windows. t r and sed norm ally are unavailable under Windows.

1.23.4 See Also