1

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

Jeroen Smink
  • 179
  • 1
  • 10
  • Shell does not have multi-dimensional arrays. How do you intend to map a 2D table into a Bash array? – Léa Gris Aug 28 '20 at 10:59
  • @JeroenSmink : I don't understand, how the resulting array should look like in your case, but it seems that your main concern here is parsing the input string, and not constructing the array. Since your fields are the same size for each line, you could us simple substring selection. Alternatively, you could do a pattern match for the column delimiter, which seems to be a vertical bar. – user1934428 Aug 28 '20 at 12:39

1 Answers1

3

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
Léa Gris
  • 17,497
  • 4
  • 32
  • 41
  • 2
    @JeroenSmink Don't miss the `--format=json`, that would be nicely and reliably handled by any kind of JSON parser in various languages like: python, perl or even `jq` for shell. – Léa Gris Aug 28 '20 at 11:33
  • Yes, Im adding the JQ package to my Docker Image as we speak. already found a great way to filter out the value's with the name key – Jeroen Smink Aug 28 '20 at 11:37