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

Updated:   |  Perl (Scripting) articles

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



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).

perl -i -pe 's/\r$//g;' foo.txt


The same can be done on Linux using the sed command.

Did you find this article helpful?

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


Add a Comment

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