68user's page 掲示板

Prev< No. 3484> Next  [最新発言に戻る] [過去ログ一覧]
No. 3484 # zsh 2003/12/24 (水) 18:16:33
>>3482
桁数の上限がわからないと駄目ですが
こんなのでどうでしょ?(下では8桁まで)

------
$ cat list
123 file1.txt
123456 file2.txt
1234567 file3.txt
$ cat test.awk
#!/bin/nawk -f
{
                i = sprintf("%d",length($1) / 3) ;
                j = sprintf("%d",length($1) % 3) ;
                if ( i > 1 ) {
                                if ( j == 0 ) j = 3 ;
                                str_buf = sprintf("%s",substr($1,0,j)) ;
                                for ( k=0 ; k<i ; k++ ) {
                                                cut_str = substr($1,j+(k*3)+1,3) ;
                                                if ( cut_str == "" ) break ;
                                                str_buf = sprintf("%s,%s",str_buf,cut_str) ;
                                                cut_str = "" ;
                                }
                } else {
                                str_buf = $1 ;
                }
                printf("%10s %s\n",str_buf,$2) ;
}

$ ./test.awk list
              123 file1.txt
      123,456 file2.txt
  1,234,567 file3.txt

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