Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and remove DOS line endings on Ubuntu

Tags:

grep

bash

unix

sed

awk

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

like image 651
exvance Avatar asked Sep 20 '12 08:09

exvance


People also ask

How to get line endings for Mac and DOS files?

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.

What is the difference between DOS/Windows line endings and Unix 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.

How to convert line endings in a text file to Unix?

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:

Why do my text files have different line terminators in Ubuntu?

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


1 Answers

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

like image 165
Jim McNeely Avatar answered Sep 20 '22 17:09

Jim McNeely