The following awk
script will go through the fields (columns) of each line and look for the /config:
and /service:
fields. When found, the full content of these fields are stored in variables.
Once the fields have been processed, the script outputs the data in the second field together with the found fields from the loop. The process then continues with the next line of input.
{
config = service = "";
for (i = 3; i <= NF; ++i) {
if ($i ~ "^/config:") {
config = $i;
} else if ($i ~ "^/service:") {
service = $i;
}
}
print $2, config, service;
}
This script has been tested and works with gawk
(GNU awk
), mawk
(Mike's awk
) and nawk
(BSD awk
).
Running this on the data that you supplied:
$ awk -f script.awk data
345 /config:launcher.mxres /service:session
4986 /config:launcher5.mxres /service:engine
912 /config:launcher_binary.mxres /service:scanner
If you want tab-delimited output, add BEGIN { OFS = "\t" }
at the top of the script.
... or you could pass the output of the original script through column -t
(will insert multiple spaces if needed to align columns):
$ awk -f script.awk data | column -t
345 /config:launcher.mxres /service:session
4986 /config:launcher5.mxres /service:engine
912 /config:launcher_binary.mxres /service:scanner
As a one-liner:
$ awk '{ config = service = ""; for (i = 1; i <= NF; ++i) { if ($i ~ "^/config:") { config = $i } else if ($i ~ "^/service:") { service = $i } } print $2, config, service }' data | column -t