Remove Lines After Pattern Match Using Linux awk

This tutorial will guide you through various examples of removing lines based on pattern matches using awk in Linux.

You’ll learn how to remove a single line after a match (inclusive and exclusive), remove lines after multiple patterns are detected, and more.

 

 

Remove Line After a Match

Let’s say the file data.txt that looks like this:

Entry 1: Data Point A
Entry 2: Data Point B
Pattern Line
Entry 3: Data Point C
Entry 4: Data Point D

To remove the line after “Pattern Line”, you can use awk like this:

awk '/Pattern Line/{print; getline; next}1' data.txt

Output:

Entry 1: Data Point A
Entry 2: Data Point B
Pattern Line
Entry 4: Data Point D

This awk command searches for “Pattern Line”. Once found, it prints the line, fetches the next line (which gets skipped), and then continues with the rest of the file.

 

Remove the Matched Line Too

To remove “Pattern Line” and the line immediately following it, you can use awk to search for “Pattern Line” and once found, awk fetches the next line (skipping it in the process) and doesn’t print the matched line:

awk '/Pattern Line/{getline; next}1' data.txt

Output:

Entry 1: Data Point A
Entry 2: Data Point B
Entry 4: Data Point D

 

Remove All Lines After a Match

To remove all lines after “Pattern Line”, you can search for “Pattern Line” and if found, you can print the line and then exit to ignore all subsequent lines in the file:

awk '/Pattern Line/{print; exit}1' data.txt

Output:

Entry 1: Data Point A
Entry 2: Data Point B
Pattern Line

 

Remove N Lines

Let’s use the following data.txt:

Entry 1: Data Point A
Entry 2: Data Point B
Pattern Line
Entry 3: Data Point C
Entry 4: Data Point D
Entry 5: Data Point E

To remove the next 2 lines after “Pattern Line”, you can locate “Pattern Line” and print it, then use a for loop to skip the next 2 lines:

awk '/Pattern Line/{print; for(i=0;i<2;i++) getline; next}1' data.txt

Output:

Entry 1: Data Point A
Entry 2: Data Point B
Pattern Line
Entry 5: Data Point E

 

Remove Lines After Every Match

Considering the data.txt file with multiple instances of “Pattern Line”:

Entry 1: Data Point A
Pattern Line
Entry 2: Data Point B
Entry 3: Data Point C
Pattern Line
Entry 4: Data Point D
Entry 5: Data Point E

To remove the line immediately following each occurrence of “Pattern Line”, you can identify every instance of “Pattern Line” and print it, then fetch and skip the next line:

awk '/Pattern Line/{print; getline; next}1' data.txt

Output:

Entry 1: Data Point A
Pattern Line
Entry 3: Data Point C
Pattern Line
Entry 5: Data Point E

 

Remove Line After a Match with Condition

Let’s work with an updated version of the data.txt file:

Entry 1: Data Point A
Pattern Line
Remove This
Entry 2: Data Point B
Pattern Line
Keep This
Entry 3: Data Point C

Suppose you want to remove the line after “Pattern Line”, but only if it contains “Remove”:

awk '/Pattern Line/{print; getline; if(/Remove/) next}1' data.txt

Output:

Entry 1: Data Point A
Pattern Line
Entry 2: Data Point B
Pattern Line
Keep This
Entry 3: Data Point C

This command looks for “Pattern Line” and prints it. It then reads the next line, and if this line contains “Remove”, it skips it, otherwise, it prints it.

 

Remove Lines After Multiple Matched Patterns

Let’s consider an expanded version of data.txt, containing multiple patterns:

Entry 1: Data Point A
First Pattern
Second Pattern
Entry 2: Data Point B
Entry 3: Data Point C
First Pattern
Entry 4: Data Point D
Second Pattern
Entry 5: Data Point E

If you want to remove lines after “First Pattern” followed by “Second Pattern”:

awk '/First Pattern/ { f1=1; next } /Second Pattern/ { f2=1; next } !(f1 && f2)' data.txt

Output:

Entry 1: Data Point A

In this command, /First Pattern/ { f1=1; next } checks each line of the input file (data.txt) for First Pattern.

If pattern1 is found in a line, the script sets a flag variable f1 to 1 and then moves to the next line of the file without executing any more of the script on the current line.

Similarly, /Second Pattern/ { f2=1; next }checks for Second Pattern in each line.

If Second Pattern is found, the script sets another flag variable f2 to 1 and then skips to the next line.

The condition !(f1 && f2) checked for each line. It tests whether both f1 and f2 are not true (i.e., not both set to 1).

If either f1 or f2 is 0 (which means First Pattern or Second Pattern or both were not found in the line), the line is printed; otherwise, it’s skipped.

Leave a Reply

Your email address will not be published. Required fields are marked *