'awk command with regex expression from variable fails
I am trying to fetch elements from array on the basis specific pattern for example: alllist is array contains following data:
echo "${alllist[@]}":
setup-demo1-release-0 setup-demo1-release-1 setup-demo1-release-2 setup-demo1-production-0 setup-demo1-production-1
where type is a variable and can have values like -release-0 and -production-0 ending with hyphen and numeric number
setup-demo1 is same for all elements in array and <-release-0> and <-production-0> so created a regex expression in which it will look for something like this: for type=release it shall return all values
setup-demo1-release-0 setup-demo1-release-1
for type=production it shall return all values
setup-demo1-production-0 setup-demo1-production-1
Note: setup-demo1 can also be written as setup-release1 or setup-production1 so using grep for these words shall be ignored.
I have also tried achieving this through awk command:
endWithDigitPattern='[-0-9]'
targetNodes=($(echo "$alllist"| sed 's/[][",]//g' | tr ' ' '\n' | awk -v nTypes="release""$endWithDigitPattern" '$1~ nTypes {print $1}'))
and it is not returning expected result while executed through script file. and may be I am not using endWithDigitPattern variable in right way.
Let me know what's wrong with my command used.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
