Name State Dns
DeltaService running DeltaService.test.qa.domain.com
DeltaService_1 stopped
DeltaService_2 stopped DeltaService_2.test.qa.domain.com
UnitedService running UnitedService.test.qa.domain.com
UnitedService_1 stopped
UnitedService_2 running UnitedService_2.test.qa.domain.com
The above data shows as part of my logs during execution. What I want to do is to search the logs for services with a similar name that have a DNS and store them in an array using bash script. For example if I want DeltaService, I want to be able to retrieve "DeltaService" and "DeltaService_2" and store in an array where I can use those service names for further processing. How can I achieve this? What I have tried:
#"${serviceLogs[@]}" = contains the structured data.
print -- '%s\n' "${serviceLogs[@]}" | grep -oh "\w*DeltaService\w*"
output:
DeltaService
DeltaService_1
DeltaService_2
#I don't need DeltaService_1 because it has no DNS.
CodePudding user response:
awk
seems to be the right tool for that:
printf -- '%s\n' "${serviceLogs[@]}" |
awk '$1 ~ /^DeltaService/ && $3 != "" {print $1}'
DeltaService
DeltaService_2
CodePudding user response:
Per OP's comments:
serviceLogs='Name State Dns
DeltaService running DeltaService.test.qa.domain.com
DeltaService_1 stopped
DeltaService_2 stopped DeltaService_2.test.qa.domain.com
UnitedService running UnitedService.test.qa.domain.com
UnitedService_1 stopped
UnitedService_2 running UnitedService_2.test.qa.domain.com'
One idea using awk
to parse the data:
$ awk -v ptn="DeltaService" '$1~ptn && $3 {print $1}' <<< "${serviceLogs}"
DeltaService
DeltaService_2
Storing the results in a bash
array named services[]
:
$ services=( $(awk -v ptn="DeltaService" '$1~ptn && $3 {print $1}' <<< "${serviceLogs}") )
$ typeset -p services
declare -a services=([0]="DeltaService" [1]="DeltaService_2")