4

I'm trying to parse a usage message like:

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help
...
Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
...

to grab the Command names only. So I'm looking to skip all lines up to and including the Commands: line, then print the first word on all following lines, i.e.

  build
  bundle
  ...

Currently I'm doing

docker-compose --help | sed -e '1,/Commands:/d' | awk '{ print $1 }'

and while this works, I suspect I could do the whole thing with a single awk. The closest I've got so far is:

docker-compose --help | awk '/Commands:/,0 { print $1 }'

But that includes the matched Commands: line. Can it be done?

9

If you mark the presence of your fence, then you can use it to decide to print the next line and after like:

awk 'x==1 {print $1} /Commands:/ {x=1}'
  • This is great, thanks. It looks like I don't even need the ,0 anymore. – ivan Jun 20 '17 at 1:26
  • 1
    note that print $1 only prints the first word. use print or print $0 to print the whole line – Chris Maes Aug 29 '18 at 12:15
2

Note that 1,/Commands:/d easily translates to awk like:

awk 'NR==1, $1 == "Commands:" {next}; NF {print $1}'

A difference with sed is that it will also work if Command: is on the first line.

And the NF {print $1} can be translated to sed:

sed -n '1,/^Commands:/!s/^[[:blank:]]*\([^[:blank:]]\{1,\}\).*/\1/p'
0
sed -n '/Commands:/,$p' filename | awk 'NR!=1{print $1}'

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.