0

I am trying to add two new columns to a csv file, that contain the multiplication of the last column to a constant. This last column is formed by the pound symbol and followed by a number.

Input:

id,tester,company,chief,previous_test,test,date,result,cost 6582983b-61d4-4371-912d-bbc76bb8208b,Audrey Feest,Pagac-Gorczany,Claudine Moakson,18/02/2019,Passwords,20/05/2020,none,£11897.96 

Expected Output:

id,tester,company,chief,previous_test,test,date,result,cost,euro,dollar 6582983b-61d4-4371-912d-bbc76bb8208b,Audrey Feest,Pagac-Gorczany,Claudine Moakson,18/02/2019,Passwords,20/05/2020,none,¬£11897.96,13682.65€,$16538.16 

What I've done so far is take a substring of this column and create another two with it, but all I get is two copies of the last column without the pound symbol:

awk -F, -v OFS="," 'NR==1 { print $0,"euro,dollar"; next } { w = substr($9, 2); u = substr($9, 2); print $0, w, u }' file.csv 

All I have to do now is multiply those 2 columns by a constant each (1.15 and 1.39 respectively) and add the symbols to each column (€ and $), but I get stuck using the print command at the end.

1 Answer 1

0
awk 'BEGIN{ FS=OFS=","; CONVFMT="%.2f" } { $(NF+1)=(NR>1? substr($9,3)*1.15"€":"euro") $(NF+1)=(NR>1? "$"substr($9,3)*1.39 :"dollar") }1' infile 

Read what is the use of CONVFMT?

2
  • I get an error awk: cmd. line:1: ^syntax error pointing to =NR>1. Commented May 2, 2021 at 21:24
  • 1
    @LlemColop a common problem with unparenthesized ternary expressions is they produce syntax errors in some awks in some contexts. Change each =NR>1 ? foo : bar to =(NR>1 ? foo : bar). Rule of thumb - always parenthesize your ternary expressions because that always makes the code clearer as well as more portable and then you don't have to think about which situations actually require the parens. Commented May 2, 2021 at 22:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.