How to get specific data from block of data based on condition
awk, bash, sed
Solution
If you know that groups are always separated by empty lines, set `RS` to the empty string:
$ awk -v RS="" '!/enable = 0/ {sub(/.*name[[:blank:]]+=[[:blank:]]+/,x);print $1}'
blue
orange
@devnull explained in his answer that GNU awk also accepts regular expressions in `RS`, so you could only split at `[group]` if it is on its own line:
gawk -v RS='(^|\n)[[]group]($|\n)' '!/enable = 0/ {sub(/.*name[[:blank:]]+=[[:blank:]]+/,x);print $1}'
This makes sure we're not splitting at evil names like
[group]
enable = 0
name = [group]
name = evil
test = more
Problem
I have a file like this: ``` [group] enable = 0 name = green test = more [group] name = blue test = home [group] value = 48 name = orange test = out ``` There may be one ore more space/tabs between label and `=` and value. Number of lines may wary in every block. I like to have the `name`, only if this is not true `enable = 0` So output should be: ``` blue orange ``` Here is what I have managed to create: ``` awk -v RS="group" '!/enable = 0/ {sub(/.*name[[:blank:]]+=[[:blank:]]+/,x);print $1}' blue orange ``` There are several fault with this: - I am not able to set `RS` to `[group]`, both this fails `RS="[group]"` and `RS="\[group\]"`. This will then fail if `name` or other labels contains `group`. - I do prefer not to use `RS` with multiple characters, since this is `gnu awk` only. Anyone have other suggestion? `sed` or `awk` and not use a long chain of commands.