Ignacio Valle
Ignacio Valle

Reputation: 11

how to grep between 2 patterns - second occurence BASH

I need to extract a section of file between two patterns, From match the first pattern until match second pattern twice

I have the next file

FileSet {
  Name = "FileseT-XXXXX"
   Include {
    Options {
      signature = MD5
     }
       File = /etc
       File = /usr/sbin
       File = /root
       File = /srv/
}
}

#SECOND FILESET
FileSet {
  Name = "FileseT-XXXXX2"
  Include {
    Options {
      signature = MD5
    }
       File = /srv/
}

}

I want to grep based on 'Name = "FileseT-XXXXX"' to the second "}" to obtain Files in section.

I want to obtain the first four "File"

    File = /etc
    File = /usr/sbin
    File = /root
    File = /srv/

Thanks in advance.

Upvotes: 1

Views: 490

Answers (2)

Ed Morton
Ed Morton

Reputation: 203502

You don't need to get as complicated as you describe just to print the File = lines in the target block. All you need is:

$ awk '/Name = /{f=/"FileseT-XXXXX"/} f && /File =/' file
       File = /etc
       File = /usr/sbin
       File = /root
       File = /srv/

If that's not all you need then edit your question to provide more truly representative sample input/output.

Upvotes: 2

marcell
marcell

Reputation: 1528

First, I would narrow down the dataset by matching data between "FileseT-XXXXX" and FileSet. Then I would extract the desired File parts.

This should do the trick to extract only the files:

pattern='XXXXX'

awk -v n="\"FileseT-$pattern\"" '$0 ~ n {flag=1; next} /FileSet/{flag=0} flag' your_data_file | awk '/File = / {print $0}'

You can save your pattern into a shell variable. Note that you can use shell variables in awk with the -v flag. This way you can easily change the desired pattern in your script. Then using awk pattern matching you can extract the lines between the "FileseT-XXXXX" and the first occurence of FileSet exluding them from the output. Then piping (|) the results to another awk call, where we extract only the desired lines.

EDIT:

To extract the lines between the "FileseT-XXXXX" and the second occurrence of }:

awk -v n="\"FileseT-$n\"" '$0 ~ n {flag=1; next} /}/ && ++c == 2 {flag=0} flag' your_data_file

Upvotes: 0

Related Questions