1

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

david
  • 11
  • 2
  • 1
    You need consistent delimiters. – Barmar Jul 27 '22 at 08:11
  • Welcome to SO, thanks for showing your efforts in form of code. Could you please do post samples of input and expected output more clearly to make your question more clear, thank you. – RavinderSingh13 Jul 27 '22 at 08:14
  • What command is `command`? `The info is obtained from tables` What tables? Is it mysql? `+-----+-----+ or` What do you mean by "or"? Does `command` _randomly_ decide which? What is command? How does it decide which output to output? Do you have affect on it? Can you manipulate options passed to this `command`? – KamilCuk Jul 27 '22 at 08:15
  • Your `read` command splits the line into individual words. You have to [set `IFS`](https://stackoverflow.com/questions/10929453/read-a-file-line-by-line-assigning-the-value-to-a-variable) properly so that you get all the words into one variable, if this is your goal. Of course the question is why you can't do everything inside your `awk` script. – user1934428 Jul 27 '22 at 08:22
  • @KamilCuk by `command` I mean the actual command that I would need to obtain the information that I need to check, since I need to use multiple, I didn know which one to choose, that said, some commands have one the "+---+---+" format, and the other would be the one im showing in the edit, which is actually the troublesome since there are no delimiters, I dont know if you require more info – david Jul 27 '22 at 08:57

2 Answers2

1

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

command | grep 'Cluster in healthy state' && { do_some_thing; }
Ivan
  • 6,188
  • 1
  • 16
  • 23
  • maybe for this case it is the best choice, but the problem is that sometimes there are multiple rows with info, and i need to be able to refer only to some of them, so i dont think that grep is a solution – david Jul 27 '22 at 10:25
0
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
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • I dont think this is what i need exactly, but its an awesome information that Ill take in mind for other purposes, thank you – david Jul 27 '22 at 10:23