You Can Use the Sed Command to Remove Unwanted Lines of Text.

The sed command is known every bit a stream editor. The sed command is used in Linux to exercise bones text transformation.

Ane of the many essential commands of Linux is besides the sed control which plays a vital role in file manipulation. It is used for many purposes; some of the master ones are as follows.

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

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

And then, if you are thinking about removing lines from a file using the sed control, please read our article from outset to end. We will give yous a brief on the methods to remove lines

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

We volition use the sed control without -i in this article because it is a demonstration article. A like method is the "dry run" selection, which shows all values for the file without making any changes.

Using the sed control, nosotros tin can remove the lines depending on the environs by using -i.

To prove the sed command, we create a file chosen LinuxhintOS.txt. In society to provide better data, we include these line numbers in the following content.

Removing Lines from a File Based on Position

This role volition explain using the sed command to remove lines from a file 'LinuxhintOS.txt' based on its position.

First, open up the file using the following command:

cat ~/<foldername>/<filename>

At present execute the beneath command to delete the first line:

In the above command:

North– "Nth" line in a file

D– deletion of the file

And so, let's execute this command into our case to delete the 10th line from the text file:

cd Documents

sed '10d' LinuxhintOS.txt

Remove the Final Line from a File

If we take to remove the concluding line from the file, instead of 'Due north' in the syntax used to a higher place, we volition employ '$.' Here, $ represents the last line.

After this, our file volition alter to something like this.

Remove the First Line and Terminal Line Together

If we desire to remove the first and last lines, we must combine the higher up two commands.

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

Every bit you can run across in the above image, the kickoff 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 merely accept to enter 'minimum' and 'maximum' line numbers.

In this instance, we will remove the lines ranging from four to vii numbers.

Later removing these ranges of lines, our file will wait similar this.

Remove Multiple Lines

sed can as well remove multiple lines in a set. As you tin can run into, we removed the third, 6th, eighth, and last lines in this example.

When applying the sed command written to a higher place, 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, nosotros remove those lines from the files which are not divers in the control, i.due east., 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 nosotros 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 volition 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 at that place are no blank or empty lines in our file, no changes accept occurred:

Remove Lines from the File Based On the Pattern

In the second part, we will see that with the assist of the sed control, how we remove lines of a similar design.

Removing Lines that Contain a Pattern

In the example, the following control removes the lines matching the "System" blueprint from the file 'LinuxhintOS.txt'.

And so, nosotros need to remove those lines which have "Not Available". That's why we will execute the below command:

sed '/Not Bachelor/d' LinuxhintOS.txt

Removing Lines that Incorporate 1 or Several Strings

Through the sed command, nosotros tin remove the lines matching the "Non Updated" or "Non Available" pattern from the file 'LinuxhintOS.txt' and that command is something like this:

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

Removing lines that starting with a specific character

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

The beneath sed command will remove all lines starting with the 'A' grapheme.

We will remove such lines from our file, which start with 'A' and 'L'. For which we volition employ the following control.

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

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

Removing Lines that Finish with a Specified Graphic symbol

This sed command removes all lines catastrophe with "m."

The following sed control will remove lines ending with both 'Thousand' and 'X' characters.

The lines of characters ending with 'Thou' and 'X' are removed from our file; then, our file will wait like this.

Removing All Lines that Outset With Uppercase

We volition remove all those lines from the file using the following control: an uppercase letter.

After applying the in a higher place command, all the lines from our file which started with capital letter letters volition be removed, and the change in our file will exist like this.

Removing a Matching Pattern Line With a Specified Range

With the help of the following command, nosotros volition remove but those lines from some divers lines with a specific pattern.

This example merely removes lines with Linux patterns from the file betwixt 1 to 6 lines.

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

Nosotros volition delete the 2nd row with the following sed command merely if it contains the "openSUSE" pattern.

sed '2{/openSUSE/d;}' file

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

The advisable command will remove the 2d line from our file 'sed – demo-ane.txt' as it has a specific pattern of "openSUSE."

Nosotros tin also delete the line matching the 'organization' blueprint and the post-obit line in the file using the sed control.

sed '/System/{N;d;}' file

Remove the Lines having Specific Patterns

Y'all 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 volition remove all those lines from the file that offset 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 control, 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 comprise any alphabetical characters.

Conclusion

This article shows several examples of removing lines from a file using the sed control. We see how with the aid of the sed command, we tin can hands remove any lines from files. If we retrieve these commands, and so we save a lot of time while reducing.

Near the author

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

smithbarriver.blogspot.com

Source: https://linuxhint.com/remove-lines-file-sed-command/

0 Response to "You Can Use the Sed Command to Remove Unwanted Lines of Text."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel