Last active
June 2, 2019 15:10
-
-
Save ytez/ce296e6931af5e7aa886ec7acc87b158 to your computer and use it in GitHub Desktop.
FPATを用いたAWKスクリプトサンプル (for @callmekohei)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/gawk -f | |
# for @callmekohei | |
BEGIN{ | |
# コンマ以外の1文字以上の連続 | |
# ないし、"以外の1文字以上の連続が""で囲われている | |
FPAT = "([^,]+|\"[^\"]+\")" | |
} | |
{ | |
# カラム数を出力 | |
printf("columns==%d; ", NF); | |
# NFの数でループし、"+" で区切って各カラムを出力 | |
for(i=1; i<NF; i++) { printf $(i) " + " } | |
print $(i) | |
} | |
# 実行サンプル | |
# $ echo 'foo,"wilkinson,tansan",baz' | ./fpat_test.awk | |
# columns==3; foo + "wilkinson,tansan" + baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#!/usr/bin/gawk -f
は、環境に合わせて読みかえてください-f FPAT='([^,]+|\"[^\"]+\")'
のかわりに、BEGIN
ブロック内で代入しています(この場合、""で囲みます)
$1
,$2
,$3
...$(NF)
を、for
ループで出力しています