Delete lines with sed linux

Deleting Specific Lines from a File in Linux Command Line

Here are a few usecases of deleting specific lines from a text file using the sed command.

The task is simple. You have to delete specific lines from a text file in Linux terminal.

Using commands like rm deletes the entire file and you don’t want that here.

You can use a text editor like Vim or Nano, enter the file and delete the desired lines. However, this approach is not suitable for automation using bash scripts.

Instead, you can use the powerful sed command line editor and delete lines that match specific criteria. And of course, you can use sed in your shell scripts.

Let me show a few use cases.

The examples shown here will modify the original file instantly. It would be a good idea to make a backup of that file before you start experimenting with it.

Remove nth line from the file

Imagine you have to delete the 7th line number in a file. You can use the sed command like this:

Let me explain how it works:

  • -i : This option enables the in-place editing. By default, sed will only display the output. With this -i option, it modifies the actual file without showing it on the display.
  • 7d : Here 7 is the line number and d instructs the deletion of the line.
  • filename : This is the file you want to modify. You may also provide absolute or relative path if the file is in some other directory.

Let me show with a real example. Take a look at the content of the file named agatha.txt:

The Mystery of the Blue Train The Seven Dials Mystery The Murder at the Vicarage Giant's Bread The Floating Admiral The Sittaford Mystery Peril at End House Lord Edgware Dies

To remove the 4th line from this file, I use:

This will result in the following display with the line ‘Giant’s Bread’ removed :

The Mystery of the Blue Train The Seven Dials Mystery The Murder at the Vicarage The Floating Admiral The Sittaford Mystery Peril at End House Lord Edgware Dies

Now, let’s say you want to remove the first line of the file in Linux command line. Use sed like this:

Читайте также:  Linux найти всех пользователей

If you want to see the result of manipulation without modifying the file itself, do not use the -i option of sed command.

Remove the last line using sed

You learned to delete a particular line but what if you need to remove the last line?

You may always get the total number of lines in a file using wc command and use this number with sed. However, sed has a dedicated way of deleting the last line of a file. You don’t need to worry about the number of files anymore now.

Delete a range of lines

Similar to what you saw earlier, you can also delete a range of lines from the file.

Suppose you have to delete the lines from 11 to 15, you can provide the range like this:

Note that it will also remove the lines 11 and 15, not just the lines that fall between them.

Remove lines containing a string

You can also use sed to delete all the lines that contain a given string or match a specific pattern.

The command below will delete all the lines that contain the word ‘string’:

Do note that this is a case-sensitive search. You may also use a regex pattern instead of a regular string.

Remove lines starting with a word

If you want to remove all the lines starting with a particular word or letter, you just have to provide the regex pattern like this:

Bonus Tip: Remove all empty lines

Before I end this article, let me quickly share how you can remove all empty lines using sed:

I hope you find this quick tip helpful in removing lines from text file in Linux command line.

If you are interested, learn more on sed command line editor:

Источник

How do I Remove Lines from a File Using the SED Command

The sed command is known as a stream editor. The sed command is used in Linux to do basic text transformation.

One of the many essential commands of Linux is also the sed command which plays a vital role in file manipulation. It is used for many purposes; some of the main ones are as follows.

Remove or delete that particular line that matches the given patterns.

  • Removing lines with regular expressions.
  • Based on a file’s position, delete a specific line.

So, if you are thinking about removing lines from a file using the sed command, please read our article from start to end. We will give you a brief on the methods to remove lines

How do I Remove Lines from a File Using the SED Command?

We will use the sed command without -i in this article because it is a demonstration article. A similar method is the “dry run” option, which shows all values for the file without making any changes.

Using the sed command, we can remove the lines depending on the environment by using -i.

To show the sed command, we create a file called LinuxhintOS.txt. In order to provide better information, we include these line numbers in the following content.

Читайте также:  Astra linux дополнительные репозитории

Removing Lines from a File Based on Position

This part will explain using the sed command to remove lines from a file ‘LinuxhintOS.txt’ based on its position.

First, open the file using the following command:

Now execute the below command to delete the first line:

N– “Nth” line in a file

D– deletion of the file

So, let’s execute this command into our example to delete the 10th line from the text file:

Remove the Last Line from a File

If we have to remove the last line from the file, instead of ‘N’ in the syntax used above, we will use ‘$.’ Here, $ represents the last line.

After this, our file will change to something like this.

Remove the First Line and Last Line Together

If we want to remove the first and last lines, we must combine the above two commands.

With this command, both our first and last lines are removed from the file simultaneously.

As you can see in the above image, the first and last line has been removed from the text file.

Remove a Range of Lines

The sed command can remove the lines of any range. For this, we just have to enter ‘minimum’ and ‘maximum’ line numbers.

In this example, we will remove the lines ranging from 4 to 7 numbers.

After removing these ranges of lines, our file will look like this.

Remove Multiple Lines

sed can also remove multiple lines in a set. As you can see, we 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’.

Removing Lines Other Than the Specified Line or Specified Range of Lines

Through the sed command, we remove those lines from the files which are not defined in the command, i.e., the lines other than the ones that are described are removed.

From this file, we will remove the remaining lines apart from the numbers 4 to 7.

Over here, the sign of “!” represents that we should not remove the line of this specific number from the file.

! – used to keep the specific number of lines from the file

After this, our file will look something like this:

Remove the Blank or Empty Lines

The sed command written below removes blank or empty lines from the appropriate file.

Since there are no blank or empty lines in our file, no changes have occurred:

Remove Lines from the File Based On the Pattern

In the second part, we will see that with the help of the sed command, how we remove lines of a similar pattern.

Removing Lines that Contain a Pattern

In the example, the following command removes the lines matching the “System” pattern from the file ‘LinuxhintOS.txt’.

So, we need to remove those lines which have “Not Available”. That’s why we will execute the below command:

Removing Lines that Contain One or Several Strings

Through the sed command, we can remove the lines matching the “Not Updated” or “Not Available” pattern from the file ‘LinuxhintOS.txt’ and that command is something like this:

Removing lines that starting with a specific character

We can remove all lines starting with any character through the sed command. We have created a new file named ‘LinuxhintOS.txt’ with the following contents:

Читайте также:  Linux параметры сетевой карты

The below sed command will remove all lines starting with the ‘A’ character.

We will remove such lines from our file, which start with ‘A’ and ‘L’. For which we will use the following command.

Using sed, we remove all lines beginning with “A” and ending with the string “Linux”.

Removing Lines that End with a Specified Character

This sed command removes all lines ending with “m.”

The following sed command will remove lines ending with both ‘M’ and ‘X’ characters.

The lines of characters ending with ‘M’ and ‘X’ are removed from our file; then, our file will look like this.

Removing All Lines that Start With Uppercase

We will remove all those lines from the file using the following command: an uppercase letter.

After applying the above command, all the lines from our file which started with uppercase letters will be removed, and the change in our file will be like this.

Removing a Matching Pattern Line With a Specified Range

With the help of the following command, we will remove only those lines from some defined lines with a specific pattern.

This example only removes lines with Linux patterns from the file between 1 to 6 lines.

After applying the command, our file ‘sed – demo-1.txt’ will look something like this.

We will delete the second row with the following sed command only if it contains the “openSUSE” pattern.

The appropriate command will remove the second line from our file ‘sed – demo-1.txt’ as it has a specific pattern of “openSUSE.”

The appropriate command will remove the second line from our file ‘sed – demo-1.txt’ as it has a specific pattern of “openSUSE.”

We can also delete the line matching the ‘system’ pattern and the following line in the file using the sed command.

Remove the Lines having Specific Patterns

You can remove all lines following the “CentOS” pattern using the sed command.

Removing Lines that Contain Numbers/Digits

With the sed command written below, we can remove all the lines containing ‘digit.’

By making some changes in the sed command, we will remove all those lines from the file that start with only digits.

After this command, all the lines that start with digits will be removed from our file.

Again, by making some changes in the command, we will remove only those lines from the file that ends with a digit.

Removing Lines that Contain Alphabetic Characters from a File

With the sed command, we will delete all lines from the file ‘LinuxhintOS.txt,’ which contain any alphabetical characters.

Conclusion

This article shows several examples of removing lines from a file using the sed command. We see how with the help of the sed command, we can easily remove any lines from files. If we remember these commands, then we save a lot of time while reducing.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.

Источник

Оцените статью
Adblock
detector