我需要帮助输出我的csv文件,我相信我有正确的格式。
这是我的csv文件:

Account number (preferred / formatted),Customer reference,Posting date,Account currency,Transaction amount
750856653,233420,3/9/2019,USD,-2092.99
750856653,233417,3/9/2019,USD,-2856.15
750856653,233426,3/9/2019,USD,-2392.25
750856653,233414,3/9/2019,USD,-1733.22

我的代码:
#!/bin/bash
input=".file path/input.csv"
while IFS=, read -r -a inputdata
do
    [[ ${inputdata[0]} =~ ^Account ]] && continue
    [[ ${inputdata[1]} == NONREF ]] && continue
    [[ ${inputdata[1]} == NONREF ]] && inputdata[1]="0"
    [[ ${inputdata[4]:0:1} == - ]] && Sign="-" || Sign="+"
    IFS=/ read -a datestamp <<<${inputdata[2]}
    printf "%s %010d %02d%02d%02d%s%012d\n" \
        "${inputdata[0]:0:3}-${inputdata[0]:3:5}-${inputdata[0]:(-1)}" \
        "${inputdata[1]}" \
        "${datestamp[1]}" "${datestamp[0]}" "${datestamp[2]:(-2)}" \
        "${Sign}" \
        "${inputdata[4]//[.-]}"
done < "${input}"

期望输出:
750-85665-3  0000233420 090319000000209299
750-85665-3  0000233417 090319000000285615
750-85665-3  0000233445 093019000000172261
750-85665-3  0000233436 093019000000169141
750-85665-3  0000233440 093019000000099289
750-85665-3  0000233459 093019000000391856

我的CSV文件:
Account number (preferred / formatted),Customer reference,Posting date,Account currency,Transaction amount
750856653,233420,3/9/2019,USD,-2092.99
750856653,233417,3/9/2019,USD,-2856.15
750856653,233426,3/9/2019,USD,-2392.25

最佳答案

编辑:你能试着用你的编辑请求跟随吗?

awk '
BEGIN{
  FS=","
}
FNR==1{
  print
  next
}
{
  $1=substr($1,1,3)"-"substr($1,4,5)"-"substr($1,length($1))
  split($3,array,"/")
  $3=sprintf("%02d%02d%s",array[2],array[1],array[3])
  gsub(/^-|\./,"",$NF)
  $3=$3"000000"$NF
  print $1,"NONREF",$3
}
' Input_file

你能试试下面的吗?
awk '
BEGIN{
  FS=","
}
FNR==1{
  print
  next
}
{
  $1=substr($1,1,3)"-"substr($1,4,5)"-"substr($1,length($1))
  $2="0000"$2
  split($3,array,"/")
  $3=sprintf("%02d%02d%s",array[2],array[1],array[3])
  gsub(/^-|\./,"",$NF)
  $3=$3"000000"$NF
  print $1,$2,$3
}
' Input_file

说明:添加上述代码的说明。
awk '                                                             ##Starting awk program here.
BEGIN{                                                            ##Starting BEGIN section here.
  FS=","                                                          ##Setting field separator as comma here.
}
FNR==1{                                                           ##Checking condition if this is 1st line then do following.
  print                                                           ##Printing current line here.
  next                                                            ##next will skip all further statements from here.
}
{
  $1=substr($1,1,3)"-"substr($1,4,5)"-"substr($1,length($1))      ##Setting $1 as sub-string(s) where 1st one starts from 1st character to till next 3 characters, 2nd one from 4th to till next 5 characters and so on.
  $2="0000"$2                                                     ##Adding 4 zeroes before value of $2.
  split($3,array,"/")                                             ##Splitting $3 into an array whose delimiter is /
  $3=sprintf("%02d%02d%s",array[2],array[1],array[3])             ##Creating $3 with values of array[2],array[1] and array[3].
  gsub(/^-|\./,"",$NF)                                            ##Globally substituting starting - and DOT with NULL in last field of current line.
  $3=$3"000000"$NF                                                ##Setting value of $3 to $3 000000 and $NF here.
  print $1,$2,$3                                                  ##Printing $1,$2 and $3 values here.
}
' Input_file                                                      ##Mentioning Input_file name here, which we are passing to awk program.

关于linux - CSV文件输出不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58543236/

10-15 16:04