9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CSV形式ファイルに列(Column)を追加するAWKスクリプト

Last updated at Posted at 2014-04-12

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
あ,い,う,と,え,お
か,き,く,と,け,こ
9
9
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
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?