Remove Line Endings From a File on Linux


Introduction

Line endings are special characters that mark the end of a line in a text file. On Unix-based systems like Linux, the line ending is represented by a single newline character ('
'). On Windows, the line ending is represented by a combination of a carriage return ('\r') and a newline character ('
'), which is referred to as a "carriage return-newline" or CRLF.

Sometimes, you may need to remove line endings from a file for various reasons. For example, you may want to remove line endings from a file before using it as input to a command that expects a single line of input, or you may want to remove line endings from a file to make it easier to read or manipulate the file.

In this article, we will look at how to remove line endings from a file on Linux using several command line tools.

Removing Line Endings Using tr command

The tr command is a utility that allows you to translate or delete characters from a file or standard input. To remove line endings from a file using tr, use the following command

$ tr -d '
' < input_file > output_file

This command reads the input_file, removes all newline characters, and writes the result to output_file. The -d option tells tr to delete the specified characters.

Here's an example of using tr to remove line endings from a file

$ cat input_file
Line 1
Line 2
Line 3
$ tr -d '
' < input_file > output_file $ cat output_file Line 1Line 2Line 3

As you can see, the line endings in the input_file have been removed and the resulting text is now a single line in the output_file.

Removing Line Endings Using sed command

The sed command is a powerful utility for editing text files. It can be used to delete or replace specific patterns in a file. To remove line endings from a file using sed, use the following command

$ sed ':a;N;$!ba;s/
//g' input_file > output_file

This command reads the input_file, removes all newline characters, and writes the result to output_file. The sed command uses the −a;N;$!ba loop to read the entire file and the s/
//g
command to delete all newline characters.

Here's an example of using sed to remove line endings from a file

$ cat input_file
Line 1
Line 2
Line 3
$ sed ':a;N;$!ba;s/
//g' input_file > output_file $ cat output_file Line 1Line 2Line 3

As you can see, the line endings in the input_file have been removed and the resulting text is now a single line in the output_file.

Removing Line Endings Using awk command

The awk command is a powerful utility for processing text files. It can be used to delete or replace specific patterns in a file. To remove line endings from a file using awk, use the following command

$ awk '{printf("%s", $0)}' input_file > output_file

This command reads the input_file, removes all newline characters, and writes the result to output_file. The awk command reads each line of the input file and prints it to the output file using the printf function. The %s format specifier tells printf to treat the input as a string, and the , operator tells awk to print the output without a line ending.

Here's an example of using awk to remove line endings from a file

$ $ cat input_file
Line 1
Line 2
Line 3
$ awk '{printf("%s", $0)}' input_file > output_file
$ cat output_file
Line 1Line 2Line 3

As you can see, the line endings in the input_file have been removed and the resulting text is now a single line in the output_file.

Conclusion

In this article, we looked at how to remove line endings from a file on Linux using three different command line tools: tr, sed, and awk. Each of these tools can be used to remove line endings from a file, and the choice of which tool to use will depend on your specific requirements and preferences. Regardless of which tool you choose, it is important to keep in mind that removing line endings from a file can make it harder to read or manipulate the file, so be sure to use this technique only when necessary.

Updated on: 04-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements