Grep lines between start & end pattern even end pattern in next file

5,094

Solution 1

Try:

$ cat file1 file2 | awk '/pattern1/,/pattern2/'

Solution 2

Using sed Version:

sed -n '/start pattern/,/end pattern/p' file1 file2

For Saving Output To New File

sed -n '/start pattern/,/end pattern/p' file1 file2 > output

Which will create for you new file called output include the desired result.

Share:
5,094

Related videos on Youtube

Vivek Nigam
Author by

Vivek Nigam

Updated on September 18, 2022

Comments

  • Vivek Nigam
    Vivek Nigam over 1 year

    My data is like:

    • First_file.txt

      Start
      abcd
      efgh
      hijk
      lmn
      
    • Second_File.txt

      opq
      rst
      uvw
      xyz
      END
      

    I want to grep all the line between patterns START & END even END pattern in next file

    Output should be like:

    abcd
    efgh
    hijk
    lmn
    opq
    rst
    uvw
    xyz
    

    • RomanPerekhrest
      RomanPerekhrest over 6 years
      what if there's Start string in Second_File.txt ?
    • Sundeep
      Sundeep over 6 years
      you need to add what you tried to solve.. anyway, you'll find an answer here (stackoverflow.com/questions/38972736/… - wasn't able to find similar here).. as far as I know, matching across files is default behavior with sed/awk
    • Vivek Nigam
      Vivek Nigam over 6 years
      no start string come again until end string found, its our application logs.
    • Murphy
      Murphy over 6 years
    • Vivek Nigam
      Vivek Nigam over 6 years
      i try this command but its work for one file :awk '/Pattern1/,/pattern2/' filename
    • αԋɱҽԃ αмєяιcαη
      αԋɱҽԃ αмєяιcαη over 6 years
      are the start pattern is in first file only and end pattern in second file only ?