Linux CSV文件输出不正确
我需要帮助我的CSV文件的输出,我相信我有正确的格式 这是我的CSV文件:Linux CSV文件输出不正确,linux,bash,Linux,Bash,我需要帮助我的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,U
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
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
我的代码:
#!/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}"
我的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
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.
编辑:请尝试以下编辑请求
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.
它起作用了!!!!!!!非常感谢,但是为什么列标题不显示呢。但是我很满意。你能在代码中加入注释吗?这样我就可以理解每一行的功能了。谢谢。@jaybiano,当然,在我的一个解决方案中添加了解释,现在干杯。我如何删除包含“NONREF”的行?我不想在输出中包含NOREF。@jaybiano,我想请您打开一个新问题,这样未来的用户就不会感到困惑,请在您打开新问题后让我知道链接,干杯。它起作用了!!!!!!!非常感谢,但是为什么列标题不显示呢。但是我很满意。你能在代码中加入注释吗?这样我就可以理解每一行的功能了。谢谢。@jaybiano,当然,在我的一个解决方案中添加了解释,现在干杯。我如何删除包含“NONREF”的行?我不希望输出中包含NOREF。@jaybiano,我想请您打开一个新问题,这样未来的用户就不会感到困惑,请在打开新问题后告诉我链接,干杯。