crimso
1
greetings,
to be clear, i have a solution but i'm wondering if anyone has a cleaner way to accomplish the following:
the variable:
LSB_MCPU_HOSTS='t70c7n120 16 t70c7n121 16 t70c7n122 16 t70c7n123 16 t70c7n124 16 t70c7n125 16 t70c7n126 16 t70c7n127 16 t70c7n128 16 t70c7n129 16 t70c7n130 16 t70c7n131 16 t70c7n132 16 t70c7n117 16 t70c7n118 16 t70c7n119 16 '
the command i used to generate the required output:
printf "%s %s\n" `echo $LSB_MCPU_HOSTS | awk '$2="15"'` | awk '{print $2, $1}' | sed "1 acp `echo ${LSB_MCPU_HOSTS} | awk '{print $1}'`"
the required output:
15 t70c7n120
cp t70c7n120
16 t70c7n121
16 t70c7n122
16 t70c7n123
16 t70c7n124
16 t70c7n125
16 t70c7n126
16 t70c7n127
16 t70c7n128
16 t70c7n129
16 t70c7n130
16 t70c7n131
16 t70c7n132
16 t70c7n117
16 t70c7n118
16 t70c7n119
thanks in advance.
Here's my attempt.
I would avoid all those pipelines and try to do all transformations in awk or sed or Perl etc.
$
$ echo $LSB_MCPU_HOSTS
t70c7n120 16 t70c7n121 16 t70c7n122 16 t70c7n123 16 t70c7n124 16 t70c7n125 16 t70c7n126 16 t70c7n127 16 t70c7n128 16 t70c7n129 16 t70c7n130 16 t70c7n131 16 t70c7n132 16 t70c7n117 16 t70c7n118 16 t70c7n119 16
$
$ echo $LSB_MCPU_HOSTS | awk '{ $2 = "15";
for(i=1; i<=NF; i+=2) {
n = i+1;
print $n, $i;
if (i == 1){print "cp", $1}
}
}'
15 t70c7n120
cp t70c7n120
16 t70c7n121
16 t70c7n122
16 t70c7n123
16 t70c7n124
16 t70c7n125
16 t70c7n126
16 t70c7n127
16 t70c7n128
16 t70c7n129
16 t70c7n130
16 t70c7n131
16 t70c7n132
16 t70c7n117
16 t70c7n118
16 t70c7n119
$
$
1 Like
RudiC
3
Try also
export LSB_MCPU_HOSTS
awk 'BEGIN {n = split (ENVIRON["LSB_MCPU_HOSTS"], TMP); TMP[0] = "cp"; print "15", TMP[1]; for (i=0; i<n; i+=2) print TMP, TMP[i+1] }'
15 t70c7n120
cp t70c7n120
16 t70c7n121
16 t70c7n122
16 t70c7n123
16 t70c7n124
16 t70c7n125
16 t70c7n126
16 t70c7n127
16 t70c7n128
16 t70c7n129
16 t70c7n130
16 t70c7n131
16 t70c7n132
16 t70c7n117
16 t70c7n118
16 t70c7n119
EDIT: Or.
TMP=( cp $LSB_MCPU_HOSTS ); unset TMP[-1]
printf "15 %s\n" ${TMP[1]} ; printf "%s %s\n" ${TMP[@]}
15 t70c7n120
cp t70c7n120
.
.
.
1 Like