jaybiano
jaybiano

Reputation: 35

CSV file does not output data alignment not working

CSV file output data does not align with the right columns.

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
750856653,233424,3/9/2019,USD,-1850.31
750856653,233403,3/9/2019,USD,-1850.32
750856653,233413,3/9/2019,USD,-1439.58
750856653,233431,3/9/2019,USD,-186.66

Script/code:

#!/bin/bash
input="/home/users/dcapps/bank/Monthsal.csv"
sed 's/,/:,/g' Monthsal.csv |
column -t -s: |
sed 's/ ,/,/g'

while IFS=',' read -r f1 f2 f3 f4 f5
do
   echo "$f1  $f2  $f3  $f4  $f5 "
done < "$input"

The result should look like this:

Accountnumber Customerreference Postingdate  Accountcurrency TransactionAmt 
750856653      233420           3/9/2019           USD                2092.99
750856653      233417           3/9/2019           USD                2856.15

Upvotes: 0

Views: 280

Answers (1)

Shawn
Shawn

Reputation: 52344

You want to pretty print the CSV data as a nicely formatted for humans table? column to the rescue:

$ column -s, -t 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
750856653                               233424              3/9/2019      USD               -1850.31
750856653                               233403              3/9/2019      USD               -1850.32
750856653                               233413              3/9/2019      USD               -1439.58
750856653                               233431              3/9/2019      USD               -186.66

(Which I see you're using in your script... not sure what the rest of the stuff you're doing with sed and adding/removing other delimiters is for, though, or that while loop...)

Upvotes: 1

Related Questions