Map bash table output to array

409 views Asked by At

How can i map the value's I got from the column name into an array that i can later use in my bash script?

+------------------------------+----------+-----------+---------+
| name                         | status   | update    | version |
+------------------------------+----------+-----------+---------+
| enable-jquery-migrate-helper | inactive | none      | 1.0.1   |
| gravityforms                 | inactive | none      | 2.4.17  |
| gutenberg                    | inactive | none      | 8.8.0   |
| redirection                  | inactive | none      | 4.8     |
| regenerate-thumbnails        | inactive | none      | 3.1.3   |
| safe-svg                     | inactive | none      | 1.9.9   |
| weglot                       | inactive | none      | 3.1.9   |
| wordpress-seo                | inactive | available | 14.8    |
+------------------------------+----------+-----------+---------+

I already tried the following, but this would only save the name of the headers in the table:

IFS=$'\n' read -r -d '' -a my_array < <( wp plugin list  --status=inactive --skip-plugins  && printf '\0' )

echo $my_array

 name status update version

After I have retrieved the value's I want to loop over them to add them to an array

1

There are 1 answers

2
Léa Gris On BEST ANSWER

Better use the CSV output format rather than the default table format if your intent is mapping the result with a shell or awk script:

wp plugin list --status=inactive --skip-plugins --format=csv

which would output this:

name,status,update,version
enable-jquery-migrate-helper,inactive,none,1.0.1
gravityforms,inactive,none,2.4.17 
gutenberg,inactive,none,8.8.0
redirection,inactive,none,4.8
regenerate-thumbnails,inactive none,3.1.3
safe-svg,inactive,none,1.9.9
weglot,inactive,none,3.1.9
wordpress-seo,inactive,available,14.8