Reputation: 29
I am writing a bash script which will read a CSV file and print the result in console with some string concatenation.
I am facing one issue with one string overwriting part of another when concatenating them in a bash script. Below is the whole code given and I am running it in Gitbash installed on my windows.
CSV File1 with two rows is given below
Apple,Cycle
Orange,Lamborgini
Script:
while IFS=, read -r x y
do
fruit=$x
vehicle=$y
echo "$y ran"
done < File1.csv
Actual Output:
ranle
ranborgini
Expected Output:
Cycle ran
Lamborgini ran
Upvotes: 0
Views: 313
Reputation: 4404
On Windows file probably contains CLRF windows new lines with carriage return, not Unix ones.
Check this SO question and answers on options for converting new lines in file / line / string.
Upvotes: 1
Reputation: 29
I found that the output contains carriage return that needs to be removed. For the example above this could be done using the tr tool:
while IFS=, read -r x y
do
fruit=$x
vehicle=$(echo "$y" | tr -d '\r')
echo "$y ran"
done < File1.csv
And now it is giving the expected output.
Upvotes: 2