0

I have a command, when I run it, it output a table that looks like;

Id        Name                                  File                                  OS          Version   Annotation   
10     MICKEY     [MICKEY_01_001] MICKEY/MICKEY.vmx       windows8Server64Guest   vmx-08    
13     DONALD     [DONALD_01_001] DONALD/DONALD.vmx       windows7Server64Guest   vmx-10
2      GOOFY      [GOOFY_01_001] GOOFY/GOOFY.vmx       windows9Server64Guest   vmx-09

I then store the table in an array call TABLE and list the TABLE array, the code looks like this;

readarray -t TABLE <<< "$(command)"
IFS='|'
for i in "${TABLE[@]}"
do
  echo $I
done

How do I append to the end of each array element? I want the table to be presented as following;

Id        Name                                  File                                  OS          Version   Annotation    
10     MICKEY     [MICKEY_01_001] MICKEY/MICKEY.vmx       windows8Server64Guest   vmx-08     ON    
13     DONALD     [DONALD_01_001] DONALD/DONALD.vmx       windows7Server64Guest   vmx-10.    OFF    
2      GOOFY      [GOOFY_01_001] GOOFY/GOOFY.vmx       windows9Server64Guest   vmx-09.    ON
Inian
  • 80,270
  • 14
  • 142
  • 161
user3289583
  • 25
  • 1
  • 7

2 Answers2

0

What does the command "$(command)" do? Should we assume, that one line of the output = one string = one element of the array? If so, then this should work for you:

readarray -t TABLE <<< "$(command)"
IFS='|'
for i in "${TABLE[@]}"
do
  if <condition_for_on_met>; then
     echo "$i ON"
  elif <condition_for_off_met>;then
     echo  "$i OFF"
  else
     echo  "$i"
  fi
done

But this is a general answer. You could improve your question by showing us what your input is and how is it processed before it is printed.

Yakup
  • 40
  • 5
0

If you want to append ON or OFF in your array

readarray -t TABLE <<< "$(command)"
#IFS='|' why ?
for ((i=1;i<"${#TABLE[@]}";i++))
# start i=1 to preserve header
do
  # condition to ON or OFF
  [ "${a:=OFF}" = 'ON' ] &&a='OFF'||a='ON'
  TABLE["$i"]="${TABLE["$i"]} $a"
done
for i in "${TABLE[@]}"
do
  echo "$i"
done
ctac_
  • 2,413
  • 2
  • 7
  • 17
  • Thank you for your suggestion. You suggestion didn't quite work the way I was hoping, it append the ON or OFF right at the beginning of the array element and overwrite the ID field. I'm trying to append the ON or OFF at the end of the array element string. Any suggestion I could achieve this? – user3289583 Oct 20 '17 at 08:44
  • I don't understand. At home, i replace "$(command)" by "$(cat infile)" and the ON and OFF come at the end of each array element and don't overwrite the ID field. – ctac_ Oct 20 '17 at 16:17
  • Thank you for your help and suggestion, I think it must be my environment. Your solution is the best that fits what I was asking. Thanks again. – user3289583 Oct 21 '17 at 03:53