Неправильный вывод файла CSV - PullRequest
1 голос
/ 24 октября 2019

Мне нужна помощь с выводом моего 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

1 Ответ

0 голосов
/ 24 октября 2019

РЕДАКТИРОВАТЬ: Не могли бы вы попробовать следовать с вашим отредактированным запросом.

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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...