Convert Windows newlines to Linux with sed and awk

If you are working with text files on both Windows and Linux environments, you may run into the issue of different newline characters. Windows uses a combination of two characters (carriage return and line feed) as newline, while Linux only uses a line feed character.

To convert Windows newlines to Linux format, you can use the command line tools sed and awk. Sed is a stream editor that can modify text files, and awk is a powerful text processing tool.

Here's an example command using sed to replace all Windows newlines in a file with Linux newlines:

sed 's/r//' inputfile.txt > outputfile.txt

This command removes the carriage return character (r) from each line in the input file, effectively converting Windows newlines to Linux format. The modified text is then saved to outputfile.txt.

Alternatively, you can use awk to achieve the same result:

awk '{ sub("r$", ""); print }' inputfile.txt > outputfile.txt

This command uses the sub() function in awk to replace the carriage return character at the end of each line with an empty string. The modified text is then printed and saved to outputfile.txt.

By using these simple commands, you can easily convert Windows newlines to Linux format and ensure your text files are compatible with both environments.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information