Remove one line from file linux

Содержание
  1. Your Own Linux.
  2. Linux How To’s | Bash Scripting | Python
  3. Monday, 20 April 2015
  4. Sed Command in Linux — Delete Lines from a File
  5. Deleting Lines from a File using sed
  6. A. sed — Delete Lines with Line Number
  7. 1. Delete ‘N’th line
  8. 2. Delete all Lines starting from ‘M’th up to ‘N’th
  9. 3. Delete Every ‘M’th Line Starting from ‘N’th Line
  10. B. sed — Delete Lines using Regular Expression/Pattern
  11. 1. Delete lines containing a specific Pattern
  12. 2. Delete all those lines not containing the Pattern
  13. 3. Delete block of lines starting from pattern matching line
  14. 4. Delete block of lines starting from Nth and ending at pattern matching line
  15. 5. Delete block of lines between two Pattern matches
  16. 6. Deleting all blank lines
  17. Unix Sed Command to Delete Lines in File — 15 Examples
  18. Sed Command to Delete Lines — Based on Position in File
  19. Sed Command to Delete Lines — Based on Pattern Match
  20. How to delete lines from a file using sed command
  21. Part-I) Removing lines based on a position in the file
  22. 1) How to delete first line from a file?
  23. 2) How to delete last line from a file?
  24. 3) Deleting a particular line from a file
  25. 4) Deleting range of lines
  26. 5) How to remove multiple lines
  27. 5.a) Deleting lines other than the specified range
  28. 6) Deleting empty or blank lines
  29. Part-II) Removing lines based on pattern match
  30. 7) Removing lines that contain a pattern
  31. 8) Deleting lines that contain one of several strings
  32. 9) Deleting lines that begin with specific character
  33. 10) Removing lines that end with specified character
  34. 11) Deleting all lines that start with uppercase
  35. 12) Deleting a matching pattern lines with specified range
  36. 12.a) Deleting only the last line if it contains a given pattern
  37. 13) How to delete pattern matching lines and also the next Line?
  38. 13.a) Deleting lines starting from a pattern till the last line
  39. 14) Deleting lines that contains Digits/Numbers
  40. 15) Deleting lines that contains Alphabetic Characters from a file
  41. Closing Notes

Your Own Linux.

Linux How To’s | Bash Scripting | Python

Monday, 20 April 2015

Sed Command in Linux — Delete Lines from a File

This is the third article of the «Super sed ‘ Series», in which we will learn how to delete lines from a file using line numbers and regular expressions. In the first two articles, we have learned, how to print lines using sed and how to append and insert lines in a file using sed. If you are familiar with sed print syntaxes, then this article would be pretty easy to understand.

For those learners who are new to sed , let’s have a brief introduction of the Super sed :

  • sed stand for Stream EDitor and it being based on the ed editor, it borrows most of the commands from the ed . It was developed by Lee E. McMahon of Bell Labs.
  • sed offers large range of text transformations that include printing lines, deleting lines, editing line in-place, search and replace, appending and inserting lines, etc.
  • sed is useful whenever you need to perform common editing operations on multiple lines without using ‘vi’ editor.
  • Whenever sed is executed on an input file or on the contents from stdin, sed reads the file line-by-line and after removing the trailing newline, places it in the «Pattern space», where the commands are executed on them after conditions (as in case of regex matching) are verified, and then printed on the stdout.

Deleting Lines from a File using sed

Before we start, just remember two points:

  1. sed «d» command lets us print specific lines based on the line number or regex provided.
  2. When ^ means beginning of the line and $ denotes end of the line, ^$ makes a «Blank Line», very useful while removing empty lines from a file.

For our better understanding, let us have a file sedtest.txt with contents as follows:

A. sed — Delete Lines with Line Number

1. Delete ‘N’th line

This will remove ‘N’th line in the FILE.txt .

Example:
To delete 1st line,

Читайте также:  Postgresql install extension windows

While, to remove last line,

If you run above commands and inspect the file sedtest.txt , you would find that, the original contents of that file would not change. In case you want to remove lines in the file and save the changes (i.e. edit the file in place), you will have to use the option -i as shown in below example:

2. Delete all Lines starting from ‘M’th up to ‘N’th

This will remove the block of lines starting at line number M and ending at line number N .

Example:
To delete block of lines from 3rd line to 8th line.

Similarly, in order to delete lines starting from 5th up to last line, you would run-

3. Delete Every ‘M’th Line Starting from ‘N’th Line

This will start from Nth line and delete every Mth line coming after that. Note that, Nth line will also be deleted.

Example:
To delete every alternate line staring from 2nd one.

B. sed — Delete Lines using Regular Expression/Pattern

1. Delete lines containing a specific Pattern

This will delete all those lines that contain the pattern provided.

2. Delete all those lines not containing the Pattern

This will delete all those lines which do not include the pattern provided.

3. Delete block of lines starting from pattern matching line

This will remove the lines from the one where pattern matches, till ‘N’th line.

Similarly, so as to delete all the lines starting from pattern matching line till the end, you would use ‘/PATTERN/,$ d’ as follows —

4. Delete block of lines starting from Nth and ending at pattern matching line

This will delete the lines starting from the ‘N’th line, till the one where pattern matches.

5. Delete block of lines between two Pattern matches

This will start deleting lines from 1st pattern match till 2nd pattern match.

6. Deleting all blank lines

7. Deleting lines containing either of two patterns

That was all about the third article on sed command. More articles on sed are coming soon. So, stay tuned. Of course, do not forget to share your feedback in the comment section below.

Источник

Unix Sed Command to Delete Lines in File — 15 Examples

Sed Command to Delete Lines: Sed command can be used to delete or remove specific lines which matches a given pattern or in a particular position in a file. Here we will see how to delete lines using sed command with various examples.

The following file contains a sample data which is used as input file in all the examples:

Sed Command to Delete Lines — Based on Position in File

In the following examples, the sed command removes the lines in file that are in a particular position in a file.

1. Delete first line or header line

The d option in sed command is used to delete a line. The syntax for deleting a line is:

Here N indicates Nth line in a file. In the following example, the sed command removes the first line in a file.

2. Delete last line or footer line or trailer line

The following sed command is used to remove the footer line in a file. The $ indicates the last line of a file.

3. Delete particular line

This is similar to the first example. The below sed command removes the second line in a file.

4. Delete range of lines

The sed command can be used to delete a range of lines. The syntax is shown below:

Here m and n are min and max line numbers. The sed command removes the lines from m to n in the file. The following sed command deletes the lines ranging from 2 to 4:

5. Delete lines other than the first line or header line

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

6. Delete lines other than last line or footer line

7. Delete lines other than the specified range

Here the sed command removes lines other than 2nd, 3rd and 4th.

Читайте также:  Scptoolkit driver windows 10

8. Delete first and last line

You can specify the list of lines you want to remove in sed command with semicolon as a delimiter.

9. Delete empty lines or blank lines

The ^$ indicates sed command to delete empty lines. However, this sed do not remove the lines that contain spaces.

Sed Command to Delete Lines — Based on Pattern Match

In the following examples, the sed command deletes the lines in file which match the given pattern.

10. Delete lines that begin with specified character

^ is to specify the starting of the line. Above sed command removes all the lines that start with character ‘u’.

11. Delete lines that end with specified character

$ is to indicate the end of the line. The above command deletes all the lines that end with character ‘x’.

12. Delete lines which are in upper case or capital letters

13. Delete lines that contain a pattern

14. Delete lines starting from a pattern till the last line

Here the sed command removes the line that matches the pattern fedora and also deletes all the lines to the end of the file which appear next to this matching line.

15. Delete last line only if it contains the pattern

Here $ indicates the last line. If you want to delete Nth line only if it contains a pattern, then in place of $ place the line number.

Note: In all the above examples, the sed command prints the contents of the file on the unix or linux terminal by removing the lines. However the sed command does not remove the lines from the source file. To Remove the lines from the source file itself, use the -i option with sed command.

If you dont wish to delete the lines from the original source file you can redirect the output of the sed command to another file.

If you like this article, then please share it or click on the google +1 button.

Источник

How to delete lines from a file using sed command

sed command stands for Stream Editor. It is used to perform basic text transformations in Linux.

sed is one of the important command, which plays major role in file manipulations. It can be used for many purposes some of which are listed below:

  • To delete or remove specific lines which matches with given pattern.
  • To remove a particular line based on position in a file.
  • Removing lines using regular expressions.

There are 27 examples listed in this article, which will help you to become a master in sed command.

If you could memorize these commands, it will save a lot of your time when you have the requirement to perform various admin tasks in your projects.

Also, check this article on how you can use the sed command to replace a matching string in a file.

Note: Since this is a demonstration article, we use sed command without the -i option, which is similar to the “dry run” option, and will display the actual output without making any changes in the file.

But, if you would like to remove the lines from the source file in real environment then use the -i option with sed command.

To demonstrate sed command, we have created the ‘sed-demo.txt’ file and added the following contents with line numbers for better understanding.

Part-I) Removing lines based on a position in the file

The first part shows how to use sed command to delete lines based on a position in the file.

1) How to delete first line from a file?

To delete the first line from a file, use the following syntax:

Syntax:

  • N – Denotes ‘Nth’ line in a file
  • d – Indicates the deletion of a line.

The below sed command removes the first line from the ‘sed-demo.txt’ file:

2) How to delete last line from a file?

If you would like to delete the last line from a file, use the following syntax ( $ denotes the last line of a file).

Читайте также:  Планшет windows 10 8gb

The below sed command removes the last line from the ‘sed-demo.txt’ file:

The following sed command removes the first and last line from the file:

3) Deleting a particular line from a file

In this example, we will delete the third line from the ‘sed-demo.txt’ file using sed command as shown below. Similarly, any line can be removed by entering the line number instead of the number 3.

4) Deleting range of lines

The sed command removes any range of given lines from a file. We need to enter the ‘minimum’ and ‘maximum’ line numbers. The below example removes lines ranging from 5 to 7.

5) How to remove multiple lines

The sed command is capable of removing a set of given lines.

In this example, the following sed command will remove 1st , 5th , 9th , and the last line.

5.a) Deleting lines other than the specified range

Use the following sed command to remove all the lines from the file, except the specified range of lines:

Details;

  • ! – Negation operator is used to keep the specified lines

Use the below command to delete all lines other than the first line:

Use the below command to delete all lines except the last line:

6) Deleting empty or blank lines

The following sed command will remove the empty or blank lines from ‘sed-demo.txt’ file.

Part-II) Removing lines based on pattern match

The second part shows, how to use sed command to remove lines in a file that match a given pattern.

7) Removing lines that contain a pattern

The following sed command will remove the lines which match the System pattern in ‘sed-demo.txt’ file .

8) Deleting lines that contain one of several strings

The following sed command removes the lines which match with the System or Linux pattern from the file ‘sed-demo.txt’:

9) Deleting lines that begin with specific character

The following sed command will remove all the lines that begin with a given character. To demonstrate this, I have created another file called ‘sed-demo-1.txt’ with following contents:

The following sed command removes all the lines that start with character R :

The following sed command will remove the lines that begin with the character either R or F :

The following sed command will remove all the lines that starts with character L and ends with System string :

10) Removing lines that end with specified character

The following sed command removes all the lines that end with character m :

The following sed command removes all the lines that ends with character either x or m :

11) Deleting all lines that start with uppercase

Use the following sed command to remove all the lines that start with an Uppercase letter:

12) Deleting a matching pattern lines with specified range

The below sed command removes the pattern Linux only if it is present in the lines from 1 to 6 :

12.a) Deleting only the last line if it contains a given pattern

The below sed command only removes the last line if it contains the pattern “openSUSE

13) How to delete pattern matching lines and also the next Line?

Use the following sed command to delete the line which matches the pattern System and the subsequent line in the file as well.

13.a) Deleting lines starting from a pattern till the last line

The below sed command removes the line that matches the pattern “CentOS”, and also deletes all the subsequent lines till the end of the file:

14) Deleting lines that contains Digits/Numbers

The below sed command removes all the lines that contains ‘digits’:

The below sed command removes all the lines which only begins with digits:

The below sed command removes all the lines which ends with digits:

15) Deleting lines that contains Alphabetic Characters from a file

The below sed command removes all the lines that contains any alphabetic characters.

Closing Notes

In this guide, we have shown you several examples to delete lines from a file using the sed command.

If you have any questions or feedback, feel free to comment below.

Источник

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