By using dspmq | awk -F '[()]' '{print $2}' I will get output in multiple lines. So I want to consider first line output as a vairable1, second line out as variable2 and so on...
How could I do that?
1 Answer
Assuming you mean awk variables, it would make more sense to use an array. Like:
dspmq | awk -F '[()]' '
{variable[NR] = $2}
END {
print "first line: "variable[1]", third line: "variable[3]
}'
If you mean shell variable, you could do:
eval "$(dspmq | awk -F '[()]' -v q="'" '
function shquote(s) {
gsub(q, q "\\" q q, s)
return q s q
}
{print "variable"NR"="shquote($2)}'
)"
If using the bash shell, you could use an array which you could fill up with:
readarray -t variable < <(dspmq | awk -F '[()]' '{print $2}')
Beware that array indices in bash (like in ksh whose design it copied) start at 0 instead of 1 (first line in ${variable[0]}).
With zsh:
variable=("${(f@)$(dspmq | awk -F '[()]' '{print $2}')}")
(first line in $variable[1])
awkscript without setting shell variables)