You can do this with a single awk command according to the following transcript:
pax> cat 1.config [Location] state=California city=Palo Alto [Outlet] id=23 manager=John Doe pax> cat 2.config [Location] state=Western Australia city=Perth [Outlet] id=24 manager=Pax Diablo pax> awk ' /^city=/ {gsub (/^city=/, "", $0); city=$0} /^manager=/{gsub(/^manager=/, "", $0); print $0 "," city} ' *.config John Doe,Palo Alto Pax Diablo,Perth
Please note that this assumes that the city arrives in front of the manager, and that all files have both the city and the manager. If these assumptions are incorrect, the awk script becomes a little more complex, but it is still possible.
In this case, it becomes something like this:
awk ' FNR==1 {city = ""; mgr = ""} /^city=/ {gsub (/^city=/, "", $0); city = $0} /^manager=/ {gsub (/^manager=/, "", $0); mgr = $0} {if (city!="" && mgr!=""){ print mgr "," city; city = ""; mgr = ""; }} ' *.config
This is to make the order inappropriate. It dumps the city and manager variables to an empty line at the beginning of each file and simply saves them when it finds the corresponding lines. After each line, if both are installed, they print and clear them.
source share