2017-11-07 8 views
1

世界の反対側の同僚が、MySQL CLIクライアントクエリの出力をテキスト形式で送信しました。彼女は私のためにそれをCSVに変換することはできません、またはINTO OUTFILE出力フラグを使用してCSVに直接出力することはできません。どのようにCSV形式に変換できますか?それは次のようになります。MySQL CLIのクエリ出力をCSVに変換するには

+-------------+------------------+------------------------------------------+-------------+ 
| pet_id  | pet_identity_id | identity_value       | pet_species | 
+-------------+------------------+------------------------------------------+-------------+ 
|  77626 |   3140819 | [email protected]     | dog   | 
|  77625 |   3140818 | [email protected]     | cat   | 
|  77622 |   3140815 | [email protected]      | aardvark | 
|  77583 |   3140776 | [email protected]     | cow   | 
+-------------+------------------+------------------------------------------+-------------+ 
4 rows in set (0.01 sec) 

私はそれがこのCSV形式のように見えるようにしたい(どちらかのパイプ - またはカンマ区切り):私はあなたがこれをやらせる様々な質問を見つけた

"pet_id"|"pet_identity_id"|"identity_value"|"pet_species" 
77626|3140819|"[email protected]"|"dog" 
77625|3140818|"[email protected]"|"cat" 
77622|3140815|"[email protected]"|"aardvark" 
77583|3140776|"[email protected]"|"cow" 

CLIクライアントはINTO OUTFILEという表記法を使用しますが、MySQLクライアントの画面に表示されるフォームの誰かから送信されたクエリサンプルを変換するだけではありません。ここで

+0

[MySQLクエリ結果をCSV形式で出力するにはどうすればいいですか?](https://stackoverflow.com/questions/356578/how-to-output-mysql-query-results-in-csv-format) – Nic3500

+0

ありがとう@Nic3500 CLIで直接アクセスできない出力に適用されることを説明するために質問を編集しました。この場合、CLIからサンプル出力の切り取りと貼り付けが行われました。 –

+0

もう1つの同様の方法があります。http://tlug.dnho.net/?q=node/209 –

答えて

0

はちょうどそれを行うことができますsedを使って小さなシェルスクリプトです:

#!/bin/bash 
# A script to convert MySQL CLI output to CSV format 

# cat the file and pipe to the next step 
cat $1 | \ 
# grep only the lines with '|' in them 
grep "\|" | \ 
# Remove the lines which begin with '+' 
sed -e '/^+/d' | \ 
# Remove the whitespace around the '|' characters 
sed -e 's/[[:space:]]*|[[:space:]]*/|/g' | \ 
# Put a double quote before every '|' character 
sed -e 's/|\(.\{1\}\)/\"&/g' | \ 
# Put a double quote after every '|' character 
sed -e 's/\(.\{1\}\)|/&\"/g' | \ 
# Remove the extra '"|' from the beginning of each line 
sed -e 's/^\"|//g' | \ 
# Remove the extra '"' from the end of each line 
sed -e 's/\"$//g' | \ 
# Remove the '|' from the end of each line 
sed -e 's/|$/\"/g' | \ 
# Remove the quotes from any purely numeric fields 
sed -e 's/"\([[:digit:]]*\)"/\1/g' 

ただ、例えば、ファイルを保存しますconvert-mysql.shをコピーしてテキストファイルのmysql-output.txtとにMySQLの出力を貼り付けなどを実行します。

$ bash ./convert-mysql.sh mysql-output.txt

あなたにこの出力を与えること:

"pet_id"|"pet_identity_id"|"identity_value"|"pet_species" 
77626|3140819|"[email protected]"|"dog" 
77625|3140818|"[email protected]"|"cat" 
77622|3140815|"[email protected]"|"aardvark" 
77583|3140776|"[email protected]"|"cow" 

これは動作しますMac上では、さまざまなLinuxの味にsedに若干の違いがあるかもしれませんが、例えば上記のシェルスクリプトのは、私が見つけた例では[[:digit:]]+でした。

1

私は、あなたがMySQLの出力としてCSVに 'フレーム化された'テーブルを変換する方法を探していると思っています。だから、ここにも、適切な内部引用はRFC4180に従ってエスケープん私自身少しのLuaスクリプト(http://www.lua.org)があります:

local tabs,counter      --to keep track of column markers 
for line in io.lines(arg[1]) do 
    if line:match '^%+[+-]+%+$' then  --frame line 
    tabs = {} 
    counter = 0 
    for ch in line:gmatch '.' do 
     counter = counter + 1 
     if ch == '+' then tabs[#tabs+1] = counter end 
    end 
    elseif line:sub(1,1) == '|' then  --data line 
    for _,tab in ipairs(tabs) do 
     line = line:sub(1,tab-1) .. '\0' .. line:sub(tab+1) 
    end 
    line = line:gsub('%Z+', 
     function(s) 
     s = s:gsub('^%s*(.-)%s*$','%1')  --remove leading & trailing spaces (optional) 
     if s ~= '' and not s:match '^-?%d-%.?%d+$' then 
      s = '"' .. s:gsub('"','""') .. '"' --add quotes while escaping internal ones 
     end 
     return s 
     end) 
    line = line:gsub('%z','|') 
    print(line:sub(2,-2)) 
    end 
end 

これは、複数行のフィールドを除いてすべてを処理する必要があります。 処理するファイルに、最初の引数として、または標準入力/パイプ経由でファイル名で入力できます。

EDIT:エンベッドされた| (パイプ)文字。

関連する問題