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 *****
Was it useful? Share this post with the world!

17 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. cat file | awk 'BEGIN{open=0; num=0;}{if($0 ~ /BEGIN/){open=1;num=num+1;}else if($0 ~ /END/){open=0;}else{if(open==1){print $0 >> num".txt";}}}'
  2. Александр says: Reply

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

    1. VerticalSpin says: Reply

      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.

    2. Have you tried scaping the pipe?

      sed ‘/show system \| inc Software/,/sh syst | i @@@@@/f’ filename.txt >

    3. I also think the @@@@@ are supposed to be written other way in the pattern. But I haven’t checked it out.

  5. Sushil Kumar says: Reply

    Hi
    I want to do something similar, but my start pattern is a variable. How I can use replace the Startpattern if it is a variable.
    I tried this
    sed -n ‘/”$StartPattern”/,/EndPattern/p’ FileName
    This didnt work. please help

    1. sed -n ‘/'”$StartPattern”‘/,/EndPattern/p’ FileName

  6. sed -n ‘/reg/{p;}’ file.txt — please tell me to display the top and bottom line + regular expression

  7. Hi,

    Just want to print the lines between the given pattern.

    I/P
    sed -n ‘/BEGIN/,/END/p’ info.txt
    ***** BEGIN *****
    BASH is awesome
    BASH is awesome
    ***** END *****
    O/P
    BASH is awesome
    BASH is awesome

    Don’t want to print Begin and end

    1. Sandib, you can try :
      sed -n ‘/BEGIN/,/END/{//!p}’

    2. I already answered, but it was not moderated, so I do it again. Sorry if this a dup 🙂

      To ignore the begin/end patterns lines, just do:
      sed -n ‘/BEGIN/,/END/{//!p}’
      With the example given:
      $ sed -n ‘/BEGIN/,/END/{//!p}’ /tmp/info.txt
      BASH is awesome
      BASH is awesome

Leave a Reply