Search code examples
shellawkposix

AWK get a text following the header line


I have this input data:

bar
foo

requires
league/flysystem ^1.0
symfony/console ^5.1

requires (dev)
phpunit/phpunit ^8

there is no new line char in the input data after the phpunit/phpunit ^8. The input data comes from piping it to AWK.

I would like to get with two separate AWK commands entries under requires and requires (dev)

first AWK script should produce:

league/flysystem ^1.0
symfony/console ^5.1

and second

phpunit/phpunit ^8

How to write these two AWK scripts?

So far I came up with a partial answer to the first script:

awk '/^requires/,/^requires \(dev\)/ { print }'

but it returns:

requires
league/flysystem ^1.0
symfony/console ^5.1

requires (dev)

Solution

  • ... | awk 'f&&!NF{exit} f; /^requires \(dev\)$/{f=1}'
    

    change the pattern to match the first one.

    Explanation

    f&&!NF{exit} if the flag is set exit when there is an empty line

    f print lines if the flag is set

    /.../{f=1} set the flag when the pattern is found

    essentially equivalent to awk -v RS= '/pattern/{print; exit}' without printing the pattern line.