CSV形式ファイルに列(Column)を追加するAWKスクリプト
add_column.awk
#!/bin/gawk -f
#./add_column.awk col=[num] val=[value] quot=[true] [filename] > [new filename]
BEGIN{
FS=","
}
{
split($0,cols,",");
rec = ""
for (i=0;i<length(cols);i++) {
colno = (i+1)
if(colno==col) {
if(quot=="true") {
rec = rec ",¥"" val "¥""
} else {
rec = rec "," val
}
}
if (i==0) {
rec = $ + colno
} else {
rec = rec "," $ + colno
}
}
print rec
}
"う"と"く"の次の"と"を挿入してみる。
test.csv
あ,い,う,え,お
か,き,く,け,こ
[実行方法]
./add_column.awk col=4 val=と quot=false test.csv > new_test.csv
new_test.csv
あ,い,う,と,え,お
か,き,く,と,け,こ