I have a file that contains string in each line. I used this file as an input in a program that reads them automatically. The program seem to not be able to read the strings correctly. I tried to fiure out what is different than usual. The only thing I found is that the file (I take it as-is from a source) adds the newline character at the beginning of the new line, and not at the end of the previous line as I used to see and as the program could successfully read previous files.

So if I use cat -E file.txt to see the newline character I see the file contains string like this:


Note that the $ is the newline character. As a troubleshooting step, I want to change the newline position to be:


How to do this?

marked as duplicate by Kusalananda sed Apr 12 at 13:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Is the file a DOS text file with \r\n newlines? – Kusalananda Apr 12 at 13:34
  • It is retrieved from the Ineternet and saved it locally. I used it as an input to my program. As I said, it contains a list of strings. Originally it was a .csv file and I converted the extension to .txt but I used to change similar files (from other web sources) without issues. – user9371654 Apr 12 at 13:37
  • @Kusalananda Can you tell me how to check to answer your question? All what I see with cat -E is what I put in the OP. – user9371654 Apr 12 at 13:38
  • Use dos2unix filename to change the file's newlines from DOS to Unix. This is very likely your issue. I have tested it to verify that cat -E produce this kind of output for DOS text files. – Kusalananda Apr 12 at 13:41
  • @user9371654 Use the file command, if tell you if it's plain ASCII or ASCII with DOS line endings. – xenoid Apr 12 at 13:41