Why does sed (insert line) output spaces between each character?

Viewed 43

I have split a larger data file into individual 2-column files for each field. This results in something like this:

0.00 3.02211e+07
1.00 3.02211e+07
2.00 3.02211e+07
3.00 3.02211e+07
4.00 3.02211e+07
5.00 3.01295e+07
6.00 3.00608e+07
7.00 2.99768e+07

When I try to add a row via sed, sed -i '1i pressure-prof' myfile.txt the output has a space character between each character (including existing spaces). If I look in notepad++, the extra spaces appear as the ASCII "NULL". In the terminal it looks like this:

pressure-prof
 0 . 0 0   3 . 0 2 2 1 1 e + 0 7
 1 . 0 0   3 . 0 2 2 1 1 e + 0 7
 2 . 0 0   3 . 0 2 2 1 1 e + 0 7
 3 . 0 0   3 . 0 2 2 1 1 e + 0 7
 4 . 0 0   3 . 0 2 2 1 1 e + 0 7
 5 . 0 0   3 . 0 1 2 9 5 e + 0 7
 6 . 0 0   3 . 0 0 6 0 8 e + 0 7
 7 . 0 0   2 . 9 9 7 6 8 e + 0 7

This is on Windows, and I think sed is being provided by cygwin or msys2. I don't know if that has anything to do with the output format issues.

Yes, I can resort to opening up files in a text editor and just adding that way. I would like to be able to utilize sed in the future though.

Thanks for any thoughts and assistance.

1 Answers
cat myfile.txt | tr -d ' ' | sed 's/./0 /4' | sed '1s/0 //' > mf2 && mv mf2 myfile.txt

Run that after you've finished adding your rows. Using tr initially wipes all the spaces, and then sed counts to the fourth character and re-adds a space.

Related