I’m trying to parse a csv file I made with Google Spreadsheet. It’s very simple for testing purposes, and is basically:
1,2
3,4
5,6
The problem is that the csv doesn’t end in a newline character so when I cat the file in BASH, I get
MacBook-Pro:Desktop kkSlider$ cat test.csv
1,2
3,4
5,6MacBook-Pro:Desktop kkSlider$
I just want to read line by line in a BASH script using a while loop that every guide suggests, and my script looks like this:
while IFS=',' read -r last first
do
echo "$last $first"
done < test.csv
The output is:
MacBook-Pro:Desktop kkSlider$ ./test.sh
1 2
3 4
Any ideas on how I could have it read that last line and echo it?
Thanks in advance.
You can force the input to your loop to end with a newline thus:
Unfortunately, this may result in an empty line at the end of your output if the input already has a newline at the end. You can fix that with a little addition:
Another method relies on the fact that the values are being placed into the variables by the
readbut they’re just not being output because of thewhilestatement:That one works without creating another subshell to modify the input to the
whilestatement.Of course, I’m assuming here that you want to do more inside the loop that just output the values with a space rather than a comma. If that’s all you wanted to do, there are other tools better suited than a
bashread loop, such as: