Newline Issue in MacOS Terminal with Shell Script

Hi everyone,

I'm trying to run a shell script in the MacOS terminal to process text files, but I'm having trouble with newline operations. The code is as follow picture:

The goal is to process each line with grep, cut, and tr, and then output it to test.txt with each entry on a new line. However, the script isn't producing newlines between the outputs as expected. Only the first line seems to be formatted correctly, and subsequent lines are not appearing on new lines.

Does anyone have a solution or suggestion for handling newlines properly in this script on MacOS?

Thank you for your help!

First, considering this is an R-related forum, it might not be the best place to ask about shell scripts.

Second, try with:

echo -e "${line}   ${result}\n" >> test.txt

no guarantee (I don't have a Mac on hand to try), but that's the most likely explanation to me.