I have this line of code that reads a text file line by line.
The text file is sometimes generated by a Windows user, sometimes by a Unix user. Therefore, sometimes I see \r\n
at the end of the line and sometimes I see only \n
.
I want my script to be able to deal with both scenarios and reach each line separately regardless of whether the linebreak is \r
, or \n
, or \r\n
, or \n\r
.
while read -r textFileLines; do ... something ...; done < text_file.txt
This code works with \n\r
(LF CR) at the end of each line, but does NOT work when I have \r\n
at the end of the line!
TEST
Create a new text file using
Notepad++ v7.5.4
while read -r LINE; do echo "$LINE"; done < /cygdrive/d/test_text.txt
output in Terminal:
first_line second_line third_string
Why isn't the fourth_output
line not shown?
dos2unix
. – Richard Neumann Jul 17 '18 at 13:56