Home > database >  How to pick multiple values from a generated column in bash?
How to pick multiple values from a generated column in bash?

Time:07-28

Im new in bash and dont have much idea in programming (2 weeks experience), the thing is:

I have to make a script that obtains the information from columns and classify them, that is already resolved with something like this:

command | head -n  (whatever) | awk -F[whatever] '{print $1 ...}'> text.txt          #awk only if needed
cat text.txt | (while read variable[1-...]
do
    var=value
    if [[condition]]; then
       echo "message"
    fi
)

The info is obtained from tables with this formats:

------------
|title|title|     title  title
 ----- -----   or 
|info |info |     info   info
-------------

The problem comes when one column has instead of one word, multiple:

title | title
info  | the info

I need to obtain the whole string, and with the method Im using Im pretty sure its impossible, but I dont know what to do and cant seem to fin the same problem anywhere

I cant place field separators manually because the info could change and it would mess the entire output.

Edit:

The command could be, for example:

 lmctl get (options)

and the output:

    NAME    AGE    INSTANCES   READY   STATUS                      PRIMARY
   (name)   119d   3           3       Cluster in healthy state    (primary)

Here I would need to obtain "Cluster in healthy state", but dont know how

Im working on a server with redhat openstack 16, for this script, Im using openshift commands to get info from pods or stuff like that, i believe its autogenerated by the command, so I just would need to obtain the info from its output

Edit2:

The expected output (when failing) from my script would be:

[ERROR] [NAME] $VARIABLE1 [STATUS] $VARIABLE2

when OK:

$VARIABLE1(name): [OK]; Status: $VARIABLE2(status)

Here is the problem, with the actual script, the status outut would be "Cluster", only

CodePudding user response:

Do you really need to split the string into columns? Just grep it all:

command | grep 'Cluster in healthy state' && { do_some_thing; }

CodePudding user response:

The command could be, for example:

 lmctl get

From https://github.com/IBM/lmctl/blob/master/docs/command-reference/action-based-cli.md :

almost all get targets feature the -o, --output option, which allows you to determine if the result should be printed as YAML, JSON or a table (default):

You should choose the tool to output a JSON -o json and parse that json with jq or Python.

There is no silver bullet "parse all possible output combinations for me". You modify the tools that output the data to output them in machine-readable, properly formatted way.

I would need to obtain "Cluster in healthy state", but dont know how

That would be most probably something similar to just:

lmctl get ... -o json | jq .STATUS
  •  Tags:  
  • bash
  • Related