Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract sections of a file based on a "starting" and "ending" pattern

I am try to extract different sections of a text file based on a certain pattern that occurs (or reoccurs sometimes) in the text file. So far I have been able to do this by reading the file line by line and then grepping this line, but I have a feeling that this is not the most efficient way to go about doing this. Please see below for an example of the pattern:

policy-map type stackoverflow

    random lines of indented text

non-indented text (signifying the beginning of the next "section" in the file)

Is there any other way that I could go about accomplishing this? I was thinking that something like awk or sed might work, but I am not skilled enough in the use of these programs to know how to go about doing this.

like image 613
lacrosse1991 Avatar asked Oct 22 '25 23:10

lacrosse1991


1 Answers

With awk :

awk '/^starting/{l=1;print;next} /^\S/{l=0} l' file

starting is an arbitrary starting string

like image 82
Gilles Quenot Avatar answered Oct 26 '25 09:10

Gilles Quenot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!