I have found that many of my files have DOS line endings. In VI they look like this: "^M". I don't want to modify files that don't have these DOS line endings. How do I do this using a bash script? Thanks!
EV
Try file -k Short version: file -k somefile.txt will tell you. It will output with CRLF line endings for DOS/Windows line endings. It will output with LF line endings for MAC line endings.
On DOS/Windows systems, different text file line endings are used than on the Unix/Linux systems. DOS/Windows uses line endings that represented as carriage return followed by line feed (CRLF or ). However, Unix/Linux uses only line feed (LF or ) for line endings.
Dos2Unix is a package that contains dos2unix and unix2dos commands for converting line endings in a text file from DOS/Windows to Unix/Linux and vice versa. This tutorial shows how to install Dos2Unix on Ubuntu 20.04. Once installation is completed, we can check version as follows: For testing purpose, create a text file that has CRLF line endings:
Probably it is a result of inconsistency between text and OS. For Ubuntu users, you can simply use file command to see the files line ending format. foo@bar:~$ file testfile1.txt testfile.txt: ASCII text foo@bar:~$ file testfile2.txt testfile2.txt: ASCII text, with CRLF line terminators
cat origin_file.txt | sed "s/^M//" > dest_file.txt
You have to do the same thing mentioned above, ctl-V then ctl-M to get that character. This is preferable for me because it is portable across many platforms and keeps it simple within bash.
on ubuntu I also find this works:
cat origin_file.txt | sed "s/\r//" > dest_file.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With