Ashu Ashu - 3 years ago 57
Bash Question

Sed -i is creating new lines and printing output

We have a script which compares two CSV files rows and printing “match found” or
“not found” output at the end of the 2nd file for each row. It was working really good, but recently I have updated Cygwin, not sure what went wrong, now it prints the output to the next line for all rows. Something has changed with new version of Cygwin- Python, and shell

Below is the code line I am using:

sed -i "${lineNum}s/$/,Found/" file2.csv

abcd efgh ijkl
mnop qrst xyz

abcd efgh ijkl found
mnop qrst xyzzz not found

it should print as above. but it is displaying as below.

abcd efgh ijkl
mnop qrst xyzzz
not found

Your help is greatly appreciated.

Answer Source

This looks like an issue with newlines. Windows platforms use \r\n as a newline sequence, whereas UNIX platforms use \n alone. If your file contains \r\n and your code is inserting content after the \r and before the \n, that's likely to be implicated.

An easy fix is to convert your files to UNIX format first, and (optionally) back when you're done:

dos2unix file2.csv
sed -i "${lineNum}s/$/,Found/" file2.csv
unix2dos file2.csv ## if you want DOS-style newlines
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download