In most cases, when you are working with a text file, it is likely to have some blank spaces. These blank spaces may range from a few characters to a number of lines that are empty or have no information. Keeping such files with unnecessary spaces can be time-consuming and inconvenient. There may be several reasons for keeping these files, but the best way of handling them is by deleting the lines that are empty. This article explores the Unix sed command and its usage in deleting empty lines in a file.

What Is Sed?

Sed stands for stream editor. It is a non-interactive command-line editor. It is primarily used for editing and filtering files. Sed is most often used in scripting, where it is used for editing either a single file or multiple files sequentially. Sed can be used to perform a sequence of editing actions on a single file or a group of files. It can be used to delete lines and words in a file, replace words, and even add text at specific locations in a file.

In this tutorial, you will learn about Linux `sed` command to delete lines from a file based on arguments. Remember that the default command does not make any changes to original file. If you want to remove files in same file use `-i` options with commands.

1. Delete the first line from the file

You can use the ‘1d’ option to delete the first line from the file. For example, you have a CSV file and wants to remove the title line. Use the following command to delete the first line with sed.

sed '1d' file_name 

Here ‘1’ represent to first line and the ‘d’ is used to delete. So combining the ‘1d’ deletes the first line from the file. See the below example:

Sed Command to Delete Lines in a File command linux command Linux Commands sed
Sed command example to delete first line from file

You can see that the `sed` command deleted the first line from the output.

2. Delete the last line from file

Use ‘$d’ command lien parameter with sed to delete the last line or footer line from a fine. The following command will delete the last line from the file_name.csv file.

sed '$d' file_name

In the above command `$` represent the last line of the file and `d` is to delete that line.

Sed Command to Delete Lines in a File command linux command Linux Commands sed
Sed command example to delete last line from file

3. Delete a specific line by number

You can specify the line number followed by `d` to delete that specific line. For example to delete the 5th file from the file, execute:

sed '5d' file_name.csv 

4. Delete the range of lines

Sed also allows to remove a range of lines from the file. We can specify the starting line and ending line number with a command separated. Let’s see the below example:

sed '2,5d' file_name

The above command will delete line number from 2 to 5 from the given file.

5. Delete lines other than the first line

Use the negation (!) operator with d option in sed command. The following sed command removes all the lines except the header line.

sed '1!d' file_name

6. Delete lines other than the last line

Use the negation (!) operator with d option in sed command. The following sed command removes all the lines except the header line.

sed '$!d' file_name

7. Delete lines other than the specified range

Use the negation (!) operator with d option in sed command. The following sed command removes all the lines except the header line.

sed '2,5!d' file_name

8. Delete the first and the last line

sed '1d;$d' file_name

9. Delete empty lines or blank lines

The sed command can be used to delete empty lines in a text file. To do this, you can use the following command:

sed '/^$/d' file_name  

The above command deletes all empty lines from the specified file. Any line contains a single space is not considered as single line.

10. Delete lines that begin with the specified character

sed '/^t/d' numbers.csv

11. Delete lines that end with the specified character

sed '/t$/d' numbers.csv

12. Delete lines that contain a pattern

sed '/pattern/d' numbers.csv

Conclusion

There is always a need to clean the text files before further processing, as they may have unwanted characters, such as tab, newline, space, etc. Sed offers a number of ways to delete lines, words, or even characters from a file. Simply put the desired operation in the command prompt, and you are done!