Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2912

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

$
0
0
先に結論 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 実行結果を「結果の保存」→「クリップボードにコピー」します。 とりあえず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

Viewing all articles
Browse latest Browse all 2912

Trending Articles