CSV文件输出不正确 [英] CSV file output is not correct

查看:70
本文介绍了CSV文件输出不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关CSV文件输出的帮助,我相信我的格式正确.

I need help with the output of my CSV file, I believe I have the proper format.

这是我的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

推荐答案

您可以尝试按照编辑后的请求进行操作.

Could you please try following with your edited request.

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



请您尝试以下.



Could you please try following.

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

说明: 为上述代码添加说明.

Explanation: Adding explanation for above code.

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.

这篇关于CSV文件输出不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆