I'd like to extract/format some text with awk.
The source text looks like this:
Section 1:
main_command some_command1 some_subcommand1 # comment 1
main_command some_command1 some_subcommand2 # comment 2
Section 2:
main_command some_command2 some_subcommand3 # comment 3
main_command some_command2 some_subcommand4 # comment 4
Section 3:
main_command some_command3 some_subcommand5 # comment 5
main_command some_command3 some_subcommand6 # comment 6
I want to know how to:
- filter to the indented lines under
Section 2; - specify which column I want (2 or 3); and
- extract the comments (after
#).
For example, if I chose column 2 the output would be:
some_command2<tab>'comment 3'
some_command2<tab>'comment 4'
I've used awk to achieve 1 and 2:
awk -v RS='\n\n' '/^Section 2:/' "$path" | awk "/^ main_command/ {print $2}"
... but I suspect there's a better way to do it all without piping. Am open to using other tools (eg sed).