I have a challenge to take an output file ("inventory.list") in this format:
hostname1.env1.domain | abc | environment1
hostname2.env1.domain | abc | environment1
hostname3.env2.domain | abc | environment2
hostname4.env2.domain | abc | environment2
hostname5.env1.domain | def | environment1
hostname6.env2.domain | def | environment2
(6 rows)
and write it out into another file in a different format:
[abc.environment1]
hostname1.env1.domain
hostname2.env1.domain
[abc.environment2]
hostname3.env2.domain
hostname4.env2.domain
[def.environment1]
hostname5.env1.domain
[def.environment2]
hostname6.env2.domain
abc
and def
are roles assigned to servers and there can be multiple servers for each role, as well as same-named roles but in different environments. I have to break out each hostname into unique groups of [role.environment], and additionally, completely delete the final line of the file which is a row count (this file is an output from an sql query).
I can read the file, strip out the pipes and whitespaces and assign/output the role/environment groupings, no problem:
#! /bin/bash
while IFS='| ' read -r certname role env; do
printf '%s\n' "[""$role"".""$env""]"
done < "/tmp/inventory.list"
...which neatly gives me the role/environment group names:
[abc.environment1]
[abc.environment2]
[def.environment1]
[def.environment2]
but I cannot figure out how to print out the hostnames linked to each role/environment group underneath each group name, neither can I work out how to get my script to ignore the last row count line. I'm guessing I have to further assign my role and environment fields (second and third fields) to its own array to then refer to it to grab out the hostnames linked to each unique grouping, but I have no idea how to achieve this. Can anyone advise, please?
IFS=' |'
is well suited for this. Is there any functional difference when the space is the first character vs. being later in the value ofIFS
? – Wildcard Jan 12 '16 at 15:20"$*"
or"${array[*]}"
. I didn't put it first on purpose though. – Stéphane Chazelas Jan 12 '16 at 15:25-t
flag (assuming you're doing it from the command line) – Eric Renouf Jan 12 '16 at 15:31