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
2
  • I believe there are some existing questions you could check. For example: stackoverflow.com/questions/12744031/… Commented Oct 19, 2017 at 8:56
  • Where do ON, OFF, ON come from? Commented Oct 19, 2017 at 9:10

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.