0

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
2
  • 2
    Please also show how your actual output looks like. Please check if the alignment of the output after the formatting edit of RavinderSingh13 is what you intended. Commented Oct 21, 2019 at 12:01
  • Something like awk -F, '{ printf "%-15s %-19s %-14s %-18s %15s\n", $1, $2, $3, $4, $5 }' Monthsal.csv? (Putting the file name in a variable is pretty pointless, especially if you don't use the variable everywhere.) Commented Oct 21, 2019 at 12:06

1 Answer 1

1

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...)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.