Issue
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.
Solution
awk
seems to be the right tool for that:
printf -- '%s\n' "${serviceLogs[@]}" |
awk '$1 ~ /^DeltaService/ && $3 != "" {print $1}'
DeltaService
DeltaService_2
Answered By - Fravadona Answer Checked By - David Goodson (WPSolving Volunteer)