Using SED and AWK to Print Lines Between Two Patterns

From the following article, you’ll learn how to print lines between two patterns in bash.

I’ll show how to to extract and print strings between two patterns using sed and awk commands.

I’ve created a file with the following text.

It’ll be used in the examples below, to print text between strings with patterns.

I Love Linux
***** BEGIN *****
BASH is awesome
BASH is awesome
***** END *****
I Love Linux

Lets say we need to print only strings between two lines that contain patterns ‘BEGIN’ and ‘END’.

Print Lines Between Two Patterns with SED

With the sed command, we can specify the starting pattern and the ending pattern, to print the lines between strings with these patterns. The syntax and the example are shown below.

Syntax:

sed -n '/StartPattern/,/EndPattern/p' FileName
Option Description
-n, –quiet, –silent Suppress automatic printing of pattern space
p Print the current pattern space

Example:

sed -n '/BEGIN/,/END/p' info.txt
***** BEGIN *****
BASH is awesome
BASH is awesome
***** END *****

Print Lines Between Two Patterns with AWK

Similar to the sed command, we can specify the starting pattern and the ending pattern with the awk command.

Syntax:

awk '/StartPattern/,/EndPattern/' FileName

Example:

awk '/BEGIN/,/END/' info.txt
***** BEGIN *****
BASH is awesome
BASH is awesome
***** END *****

8 Replies to “Using SED and AWK to Print Lines Between Two Patterns”

  1. Is it possible to capture multiple occurrances and put each occurrance in separate file ??

    1. Is it possible to capture multiple occurrances and put each occurrance in separate file ??
      Did you find a way? This might help me 🙂

  2. А как распечатать весь текст, исключая эту самую помеченную часть текста?

    1. sed ‘/StartPattern/,/EndPattern/d’

  3. hi, i want to know how to do almost the same things, find words that start and end with the same pattern. for example, find words that start and end with the letter ‘g’, note that each line has only one word

    1. #!/bin/bash
      if [[ $# -eq 1 && -f $1 ]]; then
      for i in $(cat ${1})
      do
      firstChar=”$(echo $i | cut -c1)”
      wordLength=”$(echo -n $i | wc -c)”
      lastChar=”$(echo $i | cut -c${wordLength})”
      if [[ “${firstChar}” = “${lastChar}” ]]; then
      echo “For word \”${i}\” – first and last characters match”
      fi
      done
      fi
      ####################################################################################3
      #!/bin/bash
      if [[ $# -eq 1 && -f $1 ]]; then
      for i in $(cat ${1})
      do
      firstChar=${i:0:1}
      lastChar=${i: -1}
      if [[ “${firstChar}” = “${lastChar}” ]]; then
      echo “For word \”${i}\” – first and last characters match”
      fi
      done
      fi
      ####################################################################################

      awk ‘{f=substr($0,0,1); l=substr($0,length($0),1); if ( f == l ) printf “%s%s\t%s\n”, “for word – “, $0,”- first and last characters match” ;}’ input.txt

  4. I have an issue in which my string its not a single string; it is a string with multiple words; in which using the example of sed works great; but not with awk;
    How can I group all that string with awk? I tried to encapsulate the string with “”, with { }, with ( ) and nothing works. Here is the example of my sed vs awk:
    – sed ‘/show system | inc Software/,/sh syst | i @@@@@/f’ filename.txt > out_file.txt
    That line works perfect; the strings are “show system | inc Software” and “sh syst | inc @@@@@”

    As for awk it doesnt work
    awk ‘/show system | inc Software/,/sh syst | i @@@@@/’ filename.txt > out_file.txt
    The output file is not what I expect. How can I do that strings to work in that command?

    1. more often than not, we will be completely off in answering questions like this as we don’t know the full use case what you are trying to do. post a sample of your file, and specify what your output need to look like.. that will give right info for someone who might be able to help.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.