Bootstrap FreeKB - Bash (Scripting) - carriage returns (\r ^M)
Bash (Scripting) - carriage returns (\r ^M)

Updated:   |  Bash (Scripting) articles

The following two characters are used to represent a carriage return.

\r
^M

 

Sometimes, the file command can be used to determine if a file has carriage returns. In this example, CRLF line terminators means the file most likely has carriage returns.

~]# file /path/to/example.py
/path/to/example.py: Python script, ASCII text executable, with CRLF line terminators

 

Let's say foo.txt is transferred from a Windows system to a Linux system. Windows will end each line with a new line (\n) and carriage return (\r). On Linux, lines only end with a new line (\n), thus sometimes you may want or need to remove the carriage return (\r) from each line.

This command will remove the carriage returns (\r).

sed -i 's/\r$//g' foo.txt

 

The same could be done using Perl.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter bf0297 in the box below so that we can be sure you are a human.