Created
August 14, 2015 03:27
-
-
Save ikemonn/a3cc111c252e86de556a to your computer and use it in GitHub Desktop.
CSVのデータに改行があった時に削除するshell
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
#! /bin/sh | |
# 概要 | |
# getlineで1行ずつ読み込んでいく。 | |
# その際に、各行のダブルクォートの数を数えて、偶数なら改行する。 | |
# 奇数であれば、改行せずに前の行とくっつける。 | |
# 次の行を読み込む際に、前の行が奇数の場合はダブルクォートの数に+1(前の行で余ったダブルクォートの数)をして偶数になるかチェックする | |
# 参考: https://www.usptomo.com/PAGE=20120819CSVJSON | |
# https://gist.github.com/richmikan/3251311#file_csv2lfv.sh | |
if [ $# -ne 1 ]; then | |
echo "指定された引数は$#個です。" 1>&2 | |
echo "実行するには1個の引数が必要です。" 1>&2 | |
exit 1 | |
fi | |
awk ' \ | |
# ファイルの最初の行でマッチさせる \ | |
BEGIN { \ | |
while (getline line) { \ | |
s = line; \ | |
# ダブルクォートの数をチェック \ | |
gsub(/[^"]/, "", s); \ | |
# ダブルクォート数が偶数なら改行、奇数なら改行しない \ | |
if (((length(s)+cy) % 2) == 0) { \ | |
cy = 0; \ | |
printf("%s\n", line); \ | |
} else { \ | |
cy = 1; \ | |
printf("%s", line); \ | |
} \ | |
} \ | |
} \ | |
' $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment