68user's page 掲示板

Prev< No. 4581〜4582> Next  [最新発言に戻る] [過去ログ一覧]
No. 4581 # べた 2006/04/19 (水) 16:30:24
>>4580 zsh
zshさん、ありがとうございます。
一応、動いたのですが、フィールド2の値を別の
文字置き換えたいのですが、どうすればよいのですか。

ファイル11以降に「:」で区切られたデータが存在した
場合、データの数分順番に番号を振りたいのです。
なければ、デフォルトで”1”としたいのです。

-----
$ cat a
001,a2,a3,a4,a5,a6,a7,a8,a9,a10,,,,,,
002,a2,a3,a4,a5,a6,a7,a8,a9,a10,111:222,123:456,001:002,100:200,A01:A02,0:1
003,a2,a3,a4,a5,a6,a7,a8,a9,a10,111:222,123:,:,:,A01:A02,:

-----
結果
001,1,a3,a4,a5,a6,a7,a8,a9,a10,,,,,,
002,1,a3,a4,a5,a6,a7,a8,a9,a10,111,123,001,100,A01,0
002,2,a3,a4,a5,a6,a7,a8,a9,a10,222,456,002,200,A02,1
003,1,a3,a4,a5,a6,a7,a8,a9,a10,111,123,,,A01,
003,2,a3,a4,a5,a6,a7,a8,a9,a10,222,,,,A02,

No. 4582 # zsh 2006/04/19 (水) 18:42:38
>>4581 べた
適当に直してみました。
------
BEGIN{
        FS=",";
        chk_col=11;
} {
        str_head_1=$1;
        if($chk_col ~ /:/) {
                str_head_2=head_string(chk_col);
                split_num=split($chk_col, str_foot, ":");
                for (i=1; i<=split_num; i++) {
                        for (j=chk_col+1; j<=NF; j++) {
                                split($j, col_buf, ":");
                                str_foot[i]=sprintf("%s,%s", str_foot[i], col_buf[i]);
                        }
                        print str_head_1 "," i "," str_head_2 "," str_foot[i];
                }
        } else {
                str_head_2=head_string(NF);
                print str_head_1 ",1," str_head_2;
        }
} function head_string(chk_point) {
        str_buf=$3;
        for (k=3; k<chk_point; k++) {
                str_buf=sprintf("%s,%s", str_buf, $k);
        }
        return str_buf;
}
------

Prev< No. 4581〜4582> Next  [最新発言に戻る] [過去ログ一覧]