Print Lines Between Two Patterns in Linux

Printing lines between two patterns is a common text processing task in Linux when you need to extract specific sections from files. While the grep command can find lines matching patterns, it cannot easily extract content between two different patterns. For this purpose, sed and awk commands provide powerful pattern-matching capabilities.

This article demonstrates various methods to print lines between two patterns using sed and awk commands with different conditions and filtering options.

Sample File Setup

First, let's create an example file to demonstrate the pattern matching techniques

cat example.txt
Information Start
Professional Details: 
Name: John Novik
Occupation: Technical Writer
Salary: 250K/Year
Company Name: Tutorialspoint 
Information End

Information Start
Personal Details: 
Address: House No. XX Street 254/76 
Age: 26 Years
Information End

This file contains two sections delimited by Information Start and Information End patterns. We'll use various commands to extract content between these patterns.

Using the Sed Command

The sed (stream editor) command modifies and processes text based on specified patterns and commands. It uses the -n option to suppress default output and p to print specific lines.

Including Both Patterns

To print lines between two patterns including both boundary patterns

sed -n '/pattern1/, /pattern2/p' filename

Example

sed -n '/Information Start/, /Information End/p' example.txt
Information Start
Professional Details: 
Name: John Novik
Occupation: Technical Writer
Salary: 250K/Year
Company Name: Tutorialspoint 
Information End

Information Start
Personal Details: 
Address: House No. XX Street 254/76 
Age: 26 Years
Information End

Excluding One Pattern

To include only the starting pattern while excluding the ending pattern

sed -n '/pattern1/, /pattern2/{ /pattern2/!p }' filename

Example

sed -n '/Information Start/, /Information End/{ /Information End/!p }' example.txt
Information Start
Professional Details: 
Name: John Novik
Occupation: Technical Writer
Salary: 250K/Year
Company Name: Tutorialspoint 

Information Start
Personal Details: 
Address: House No. XX Street 254/76 
Age: 26 Years

Excluding Both Patterns

To print only the content between patterns without including the boundary patterns

sed -n '/pattern1/, /pattern2/{ /pattern1/! { /pattern2/! p } }' filename

Using the Awk Command

The awk command provides more flexible pattern processing with built-in variables and programming constructs. It's particularly useful for complex text processing tasks.

Basic Range Pattern

The simplest method to print lines between two patterns

awk '/pattern1/, /pattern2/' filename

Example

awk '/Information Start/, /Information End/' example.txt

Using Flag Variables

A more flexible approach using a flag variable to control printing

awk '/pattern1/{ f = 1 } f; /pattern2/{ f = 0 }' filename

This method

  • Sets flag f = 1 when the first pattern is found

  • Prints all lines while f is true

  • Resets flag f = 0 when the second pattern is found

Excluding Both Patterns

To print content between patterns without including the boundary patterns

awk '/pattern1/{ f = 1; next } /pattern2/{ f = 0 } f' filename

The next statement skips processing the current line, effectively excluding the first pattern from output.

Specific Line Range

To print lines within a specific line number range

awk 'NR==X, NR==Y' filename

Where NR is the built-in variable for line numbers, X is the starting line, and Y is the ending line.

Example

awk 'NR==5, NR==11' example.txt

Comparison of Methods

Method Command Use Case Complexity
Sed Range sed -n '/p1/, /p2/p' Simple pattern extraction Low
Sed Conditional sed -n '/p1/, /p2/{ condition }' Pattern exclusion Medium
Awk Range awk '/p1/, /p2/' Basic range matching Low
Awk Flag awk '/p1/{f=1} f; /p2/{f=0}' Flexible control Medium

Conclusion

Both sed and awk provide effective methods for extracting lines between patterns in Linux. Sed is ideal for simple pattern-based extraction, while awk offers more flexibility with variables and conditional logic. Choose the appropriate method based on your specific text processing requirements and complexity needs.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements