LoginSignup
1
2

More than 3 years have passed since last update.

BigQueryの結果(TSV)をシェル芸でMarkdownにする

Posted at

先に結論

xclipがあれば

$ xclip -o |
  sed -e 's/\t/|/g' -e 2i$(xclip -o | head -n 1 | sed -e 's/\t/|/g' -e 's/\w*/--/g') |
  xclip -selection c

無い場合、bqresult.txtというファイルに結果をペーストして

$ cat bqresult.txt |
  sed -e 's/\t/|/g' -e 2i$(cat bqresult.txt | head -n 1 | sed -e 's/\t/|/g' -e 's/\w*/--/g')

動作環境

$ bash --version | head -n 1
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)

また、ここでのMarkdownはGitHub Flavored Markdown(GFM)のテーブル拡張記法に準じます。

説明

まずBigQueryの画面で好きなクエリを実行します。

今回はサンプルとして本当になんの意味もないクエリを実行します。

SELECT 1 AS id, "hoge" AS name
UNION ALL SELECT 2 AS id, "fuga" AS name
UNION ALL SELECT 3 AS id, "piyo" AS name

image.png

実行結果を「結果の保存」→「クリップボードにコピー」します。

image.png

とりあえずbqresult.txtというファイルにペーストします。

bqresult.txt
id  name
1   hoge
2   fuga
3   piyo

このクリップボードにコピーされたクエリ結果はTSV(タブ文字区切り)になっています。タブ文字をバーティカルバー(|)区切りに変換します。お好みでスペースを入れたりしてください。

$ cat bqresult.txt | sed -e 's/\t/|/g'
id|name
1|hoge
2|fuga
3|piyo

区切り行も作る必要があります。

$ cat bqresult.txt | head -n 1 | sed -e 's/\t/|/g' -e 's/\w*/--/g'
--|--

これらを組み合わせれば完成です。

$ cat bqresult.txt |
>   sed -e 's/\t/|/g' -e 2i$(cat bqresult.txt | head -n 1 | sed -e 's/\t/|/g' -e 's/\w*/--/g')
id|name
--|--
1|hoge
2|fuga
3|piyo
id name
1 hoge
2 fuga
3 piyo
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2