Search code examples
bashshellscriptingline-breaks

Newline is not '\n'


I have a text file which was created by Matlab (I don't have the source code), and was in the form:

a   b   c   d

e   f   g   h

I used

sed -i '' $'s/\t/\/g' filename

to replace all the tabs with commas and ended up have a file that looks like this:

a,b,c,d
e,f,g,h

then, I tried to remove all the line breaks using

tr '\n' ' ' < filename

It gave me only the last line, But when I manually edited the text file by placing the pointer to the end of the line and then pressing "del" and "enter" and re-ran the code it worked fine. So, the newline in the text file is probably not symbolized by \n, what other chars are there to symbolize line breaks?

P.S If I run the tr line on the file before I remove the tabs I get an empty output.

Thank you.


Solution

  • Sounds like your newlines are \r\n (Windows-style ones). One option would be to remove them first using this command:

    tr -s '\r\n' ' ' < file
    

    The -s switch means each sequence of characters present in the input is only replaced by a single space. Thanks to glenn jackman for pointing this out.

    Guessing your intention slightly, you may want to use something like this, to replace all spaces including line breaks with commas:

    tr -s '[:space:]' ',' < file
    

    You could then pipe this to sed to remove the trailing comma if you wanted.