Creating array from command output (bash shell script)
Hi, I have a command that outputs n lines of text, and I want to place each line into an array element, but I can't seem to get the syntax correct
so my command is this: cat $configfile | sed -n '/cluster:'$clustername'/,/cluster/ p' | awk /host/ which produces many lines depending on the value of $clustername. I'd like to get each line as elements of an array, any help would be much appreciated, Thanks |
Quote:
you also need to fix your sed statement and the excessive piping. Code:
#!/bin/bash |
Code:
#!/bin/bash without a representative sample of $configfile, it's hard to do better. perhaps it's posible to do it without regexes. |
Another way is by setting the IFS variable to newline only (and restore it to the default later). Following the suggestion by crts:
Code:
OLD_IFS=${IFS} |
@everToulouse - your Array is a string and not an Array. Try changing @ for a 1 and you will see there is no value stored there. Easy fix though, throw in some
brackets and your all good: Code:
if ((flag)) && [[ $line =~ host ]]; then Array+=("$line"); fi |
Hi grail,
oops! corrected, thx. |
All times are GMT -5. The time now is 01:34 PM. |