0

How can I print lines muitil patterns which meet a condition?

For instance for an input file

partially containing the following:

ID : 1

Status : Ok

Name : Lulu

Age : 18

State : US




ID : 2

Status : Ok

Name : Master Yi

Age : 22

State : UK



ID : 3

Status : Ok

Name : Garen

Age : 20

State : CA
------------

Codition : IF Age == 20 | print 
----------

ID : 3

Status : Ok

Name : Garen

Age : 20

State : CA

please help

Thanks

8
  • You talk about multi patterns, but only look for one pattern. Please elaborate by editing your question. Also, does it have to be awk?
    – Panki
    Jun 16, 2020 at 9:15
  • 1
    If this file has the fixed set of lines.. than you can do something like grep -C4 'Name : Master Yi' filename Jun 16, 2020 at 9:23
  • 1
    Please edit your question to include answers to comments. Most importantly, please use the formatting tools to format your example file as code. Does it really have so many blank lines? Are there really spaces around the :? Ware there really horizontal lines in the file? We need to know the exact format of the file to be able to help you.
    – terdon
    Jun 16, 2020 at 9:52
  • 1
    Is Codition : IF Age == 20 | print part of your input file? What is your desired output?
    – terdon
    Jun 16, 2020 at 10:08
  • 1
    Please edit your question to include the desired output for the example input you provided. Also, include what you have already tried and where you ran into difficultier.
    – AdminBee
    Jun 16, 2020 at 11:33

2 Answers 2

1

If your file is really as you show and you have 4 empty lines between each record, and 3 empty lines between each line of a record, then you can do this:

$ awk -v RS='\n\n\n\n' '/Age : 20\n/' file 
ID : 3

Status : Ok

Name : Garen

Age : 20

State : CA

You haven't clarified this, but I suspect that the extra blank lines aren't actually part of your file but only artefacts of your question's formatting, so you have only one blank line between each line of a record and only two blank lines between each record. If so, your input file is like this:

ID : 1
Status : Ok
Name : Lulu
Age : 18
State : US


ID : 2
Status : Ok
Name : Master Yi
Age : 22
State : UK

ID : 3
Status : Ok
Name : Garen
Age : 20
State : CA

If your file is indeed like this, then you can do:

$ awk -v RS= '/Age : 20\n/' file
ID : 3
Status : Ok
Name : Garen
Age : 20
State : CA
0

The following approach of first creating an array to map the tags (names) of the fields to their values lets you do what you currently want and enhance it trivially in future to test for compound conditions, print select fields, print fields in a different order or do anything else you might want to do with your tag:value type data:

$ cat tst.awk
BEGIN { OFS=" : " }
!NF { next }
{
    tag = val = $0
    sub(/ *:.*/,"",tag)
    sub(/[^:]+ *: */,"",val)
    if ( !(tag in tag2val) ) {
        tags[++numTags] = tag
    }
    tag2val[tag] = val
}
tag == "State" {
    if ( tag2val["Age"] == 20 ) {
        for (tagNr=1; tagNr<=numTags; tagNr++) {
            tag = tags[tagNr]
            val = tag2val[tag]
            print tag, val
        }
        print ""
    }
    delete tag2val
    numTags = 0
}

.

$ awk -f tst.awk file
ID : 3
Status : Ok
Name : Garen
Age : 20
State : CA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.