Text processing on CSV file

2019-07-14 01:51发布

I am dealing with a file which has the following form:

"1999-01-04";1391.12;3034.53;66.515625;86.2;441.39
"1999-01-05";1404.86;3072.41;66.3125;86.17;440.63
"1999-01-06";1435.12;3156.59;66.4375;86.32;441

Sometimes, there are values with no decimals (e.g. 441 instead of 441.0) and I need the decimals to be there. How do I write a script such that all integers are added .0 so that they become floats?

2条回答
淡お忘
2楼-- · 2019-07-14 02:09

With sed

sed 's/\(;[^\.]*\)\(;\|$\)/\1.00\2/g' file

just a simple replacement regex.

"1999-01-04";1391.12;3034.53;66.515625;86.2;441.39
"1999-01-05";1404.86;3072.41;66.3125;86.17;440.63
"1999-01-06";1435.12;3156.59;66.4375;86.32;441.00
查看更多
可以哭但决不认输i
3楼-- · 2019-07-14 02:21

Save this awk script as awk.src:

BEGIN {
  FS=";"
}
#
## MAIN Block
#
{
  printf $1; printf FS;
  for (i=2;i<=NF;i++) {
    if ($i !~ "\\.") {
      printf "%.1f", $i;
    }
    else { printf $i; }
    if (i!=NF) {
      printf FS;
    }
    else { printf "\n"; }
  }
}

Try it:

$ awk -f awk.src < sample.txt 
"1999-01-04";1391.12;3034.53;66.515625;86.2;441.39
"1999-01-05";1404.86;3072.41;66.3125;86.17;440.63
"1999-01-06";1435.12;3156.59;66.4375;86.32;441.0
查看更多
登录 后发表回答