0

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:

string1
$string2
$string3
$string4

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

string1$
string2$
string3$
string4$

How to do this?

  • Is the file a DOS text file with \r\n newlines? – Kusalananda Apr 12 '19 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 '19 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 '19 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 '19 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 '19 at 13:41
  • Incidentally, it is the newline character that determines where the line ends, so you cannot have newline characters not at a line end. – xenoid Apr 12 '19 at 13:44
  • @xenoid It's a side effect of the carriage return displacing the $ in the output from cat -E. – Kusalananda Apr 12 '19 at 13:45
  • This is the output of the file command: ASCII text, with CRLF line terminators – user9371654 Apr 12 '19 at 13:49
  • @user9371654 Yep, CRLF is "carriage return followed by line feed", i.e. a DOS text file. – Kusalananda Apr 12 '19 at 13:51

0 Answers0