How do I delete a line from a file using the SED command? Implementation in detail

The sed command is known as the stream editor. The sed command is used in Linux for basic text conversion. One of the many basic commands of Linux is also the sed command, which plays a vital role in file manipulation. It serves a variety of purposes; Some of the main ones are as follows. SED command to remove lines from the file? Delete or delete specific rows that match a given pattern.

  • Use regular expressions to delete rows.
  • Depending on the location of the file, delete specific lines.

So, if you’re thinking of using the sed command to remove lines from a file, read our article from start to finish. We’ll give you a brief overview of how to remove lines, as well as an example of how the SED command deletes lines.

How do I remove a line from a file using the SED command?

How does the SED command delete lines from a file? We’ll use the sed command without -i in this article because it’s a demo article. A similar approach is the “Test Run” option, which displays all the values of the file without any changes. Using the sed command, we can use -i to delete rows that depend on the environment. To display the sed command, we create a file called LinuxhintOS.txt. To provide better information, we have included these line numbers in the following.

Removes lines from the file based on location

This section will explain how to use the sed command to remove rows from the file “LinuxhintOS.txt” based on location. First, open the file with the following command:

cat ~/<foldername>/<filename>

SED command to remove lines from the file? Now execute the following command to delete the first line: In the above command: N – “Nth” line in the file D – Delete the file So let’s execute this command in the example to remove line 10 from the text file:

sed 'Nd' file
cd Documents

sed '10d' LinuxhintOS.txt

SED command to delete row example: Delete the last row from the file

If we have to remove the last line from the file instead of the “N” in the syntax used above, we will use “$”. Here, $ stands for the last line. After this, our files will be changed to something like this.

sed '$d' file

Delete the first and last lines together

If we want to get rid of the first and last lines, we have to combine the above two commands. With this command, our first and last lines are removed from the file at the same time.

sed 'Nd;$d' filename

As shown in the image above, the first and last lines have been removed from the text file.

Delete a series of rows

The sed command can delete rows of any range. To do this, we just need to enter the “Minimum” and “Maximum” line numbers. In this example, we’ll remove rows of 4 to 7 numbers. Once we delete the rows for these ranges, our file will look like this.

sed '4,7d' file

Delete multiple rows

How does the SED command delete lines from a file? Sed can also delete multiple rows in a group. As you can see, we’ve removed the third, sixth, eighth, and last lines in this example. When applying the sed command written above, the following changes occur in our file “LinuxhintOS.txt”.

sed '3d;6d;8d;$d' file

Deletes the specified row or the row outside the specified range

SED command to remove lines from the file? With the sed command, we remove those file lines that are not defined in the command, i.e. lines other than those described. From this file, we will remove the rest of the lines except for the numbers 4 through 7. Here, “! The flag indicates that we should not remove the line of this particular number from the file. ! – Used to preserve a specific number of lines in the file After this, our file will look like this:

Sed '4,7! d' file

Example of a SED command to delete a row: Delete a blank or blank line

The sed command written below removes blank or blank lines from the appropriate file. Since there are no blank or blank lines in our file, nothing has changed:

sed '/^$/d' file

Remove rows from the file based on the pattern

In the second part, we will see how we can delete rows with similar patterns with the help of the sed command.

Delete the row that contains the pattern

In the example, the following command removes rows from the file “LinuxhintOS.txt” that match the “System” pattern.

sed '/System/d' file

So, we need to remove those “unavailable” rows. That’s why we’re going to execute the following commands:

sed '/Not Available/d’ LinuxhintOS.txt

Delete lines that contain one or more strings

With the sed command, we can remove rows from the file “LinuxhintOS.txt” that match the “Not Updated” or “Not Available” pattern, which looks like this:

sed '/Not Updated\|Not Available/d' LinuxhintOS.txt

Delete lines that contain one or more strings

With the sed command, we can remove rows from the file “LinuxhintOS.txt” that match the “Not Updated” or “Not Available” pattern, which looks like this:

sed '/Not Updated\|Not Available/d' LinuxhintOS.txt

Delete lines that begin with a specific character

We can delete all lines that start with any character via the sed command. We have created a new file called “LinuxhintOS.txt” with the following contents:

How does the SED command delete lines from a file? The following sed command will remove all lines that begin with the ‘A’ character. We will remove these lines starting with “A” and “L” from our files. To do this, we will use the following command. With sed, we remove all lines that start with “A” and end with the string “Linux”.

sed '/^A/d' file
sed '/^[AL]/d' file
sed '/^(A).*(Linux)/d' file

Deletes lines that end with the specified characters

SED command to delete row example: This sed command deletes all lines ending with “m”. The following sed command will remove lines ending with the ‘M’ and ‘X’ characters. Remove lines of characters ending with “M” and “X” from our files; Our file will then look like this.

sed '/m$/d' file
sed '/[xm]$/d' file

Delete all lines that begin with an uppercase

We’ll remove all of these lines from the file using the following command: one uppercase letter. Once the above command is applied, all lines starting with a capital letter in our file will be deleted and the changes in our file will look like this.

sed '/^[A-Z]/d' file

Deletes a matching pattern line with a specified range

SED command to remove lines from the file? With the help of the following command, we will remove only those rows from some defined rows that have a specific pattern. This example only removes lines with Linux mode from between lines 1 and 6 in the file. After applying the command, our file “sed – demo-1.txt” will look like this. Only when the “openSUSE” mode is included will we use the following sed command to remove the second line. The appropriate command will remove the second line from our file “sed – demo-1.txt” because it has a specific “openSUSE” pattern. The appropriate command will remove the second line from our file “sed – demo-1.txt” because it has a specific “openSUSE” pattern. We can also use the sed command to delete the lines that match the System pattern and the following lines in the file.

sed '1,6{/Linux/d;} file
sed '2{/openSUSE/d;}' file
sed '/System/{N;d;}' file

Delete lines with a specific pattern

You can use the sed command to delete all rows that follow the “CentOS” pattern.

sed '/Centos/,$d' file

Delete rows that contain numbers/numbers

Using the sed command written below, we can delete all lines that contain ‘digit’. By making some changes to the sed command, we’ll remove all lines from the file that only start with a number. Once this command is executed, all lines that start with a number will be removed from our files. Again, by making some changes to the command, we will remove only those lines that end with numbers from the file.

sed '/[0-9]/d' file
sed '/^[0-9]/d' file
sed '/[0-9]$/d' file

Remove lines containing alphabetic characters from the file

How does the SED command delete lines from a file? Using the sed command, we will remove all lines containing alphabetic characters from the file “LinuxhintOS.txt”.

sed '/[A-Za-z]/d' file

An example of the SED command to delete a line

SED command to remove lines from the file? This article shows several examples of using the sed command to remove rows from a file. We see that with the help of the sed command, we can easily remove any line from the file. If we memorize these commands, then we save a lot of time while decreasing.