2

I am writing a shell script that reads a csv file and prints the read values:

#!/bin/bash     
#echo"Starting execution in script"     
exec < src/main/resources/test1.csv || exit 1      
read header # read (and ignore) the first line      
while IFS=, read name age salary ; do  
  echo  "{"\"name"\":"\"${name}"\","\"age"\":${age},"\"salary"\":${salary}}"     
done   

test1.csv (Input file):

name,age,salary   
Subhash,26.0,10000.0   
Amit,25.0,20000.0   
Rohit,28.0,3000.0   
Manoranjan,20.0,50000.0

While running the script, I am getting the output in the following way:

}"name":"Subhash","age":26.0,"salary":10000.0   
}"name":"Amit","age":25.0,"salary":20000.0   
}"name":"Rohit","age":28.0,"salary":3000.0   
}"name":"Manoranjan","age":20.0,"salary":50000.0 

However, if I try to run the same echo line from the above script on the terminal, I am getting proper output with braces:

{"name": "subhash","age":23,"salary":1000}   

What is wrong with the above script?

Rene Knop
  • 1,788
  • 3
  • 15
  • 27
G.V. Sridhar
  • 129
  • 1
  • 5

2 Answers2

3

The problem is that your test1.csv contains "windows" \r\n line return.

The \r is read as part of the last variable, and the following character (}) is then printed over the opening brace - echo is doing its job.

I am not sure why doing this interactively works for you (did you read the variables the same way?) but you just need to clean up your input files, or strip it from the end of the last variable with e.g. ${salary%$'\r'}

(As a side note, you do not need to exit your quote to print escaped quote, so your echo could look like this:

echo "{\"name\":\"${name}\",\"age\":${age},\"salary\":${salary%$'\r'}}"

)

Asmadeus
  • 300
  • 2
  • 11
2

I adapted your script a little bit; removed the exec and read header. Instead I chose sed to cut off the header line and akw to remove the trailing whitespaces (that comes from the test1.csv file). This work's very well for me:

while IFS=, read name age salary; do
  echo  "{"\"name"\":"\"${name}"\","\"age"\":${age},"\"salary"\":${salary}}"
done < <(sed 1d src/main/resources/test1.csv | awk '{$1=$1};1')
Rene Knop
  • 1,788
  • 3
  • 15
  • 27