Get specific information from output

Hello community,
I'm going crazy to analize an output via shell script and then get some information from it, here is the output:

Slot 2 - MMU2 H, RAU2 X 15/A01
 XPIC                              Enabled
 Autorestore                       Unknown
Slot 3 - MMU2 H, RAU2 X 15/A01
 XPIC                              Enabled
 Autorestore                       Unknown
Slot 4 - MMU2 H, RAU2 X 26/A05
 XPIC                              Disabled
Slot 5 - MMU2 H, RAU2 X 26/A05
 XPIC                              Enabled
 Autorestore                       Unknown
Slot 6 - MMU2 H, RAU2 X 15/A01HP
 XPIC                              Disabled

Well, when XPIC is enabled and Autorestore in NOT Enabled (in the example is Unknown but could be something else like "Disabled"), I have to take the related slot number to create a specified command (to execute it later).

So in the above example, slot 2:

  • XPIC is enabled and Autorestore is Uknown
  • Create outut command like: rl 2 xpicrecovery 5 5 (where 2 is the slot number)

Same for slot 3

  • XPIC is enabled and Autorestore is Uknown
  • Create outut command like: rl 3 xpicrecovery 5 5 (where 3 is the slot number)

Slot 4:
XPIC is disable (no autorestore variable present)
Just skip.....

So in the end I should have an output like:

rl 2 xpicrecovery 5 5
rl 3 xpicrecovery 5 5
rl 5 xpicrecovery 5 5

Could someone help please? I'm stuck at this point! :frowning:

Lucas

Please, do post your attempts and efforts, including operating system and shell details in future.

We would like to see how far you got before helping.

See if this helps you get started...

awk '/Slot/ { slot=$2 } ; /XPIC/ { status=$2 } ; $1 == "Autorestore" && $2 != "Enabled" { print "rl", slot, "xpicrecovery 5 5"  }' inputfile

Hope that helps
Regards
Peasant.

1 Like

Thank you very much Peasant, it's working fine and this is the good start point for the rest of my code!

Thank you
Lucas

Although Peasant's proposal delivers the desired output, it doesn't check the XPIC status but, if disabled, relies upon the missing Autorestore entry and silently discards the XPIC status.

Try (with a disabled slot 3 XPIC)

awk '
/Slot/                  {slot = $2
                        }
/XPIC/                  {Xstatus = $2
                        }
Xstatus == "Enabled" &&
$1 == "Autorestore" &&
$2 != "Enabled"         { print "rl", slot, "xpicrecovery 5 5"
                        }
' file
rl 2 xpicrecovery 5 5
rl 5 xpicrecovery 5 5

Guys, please help!

With the following situation (Slot 2 both enabled)

Slot 2 - MMU2 H, RAU2 X 15/A01
 XPIC                              Enabled
 Autorestore                       Enabled
Slot 3 - MMU2 H, RAU2 X 15/A01
 XPIC                              Enabled
 Autorestore                       Unknown
Slot 4 - MMU2 H, RAU2 X 26/A05
 XPIC                              Enabled
 Autorestore                       Unknown
Slot 5 - MMU2 H, RAU2 X 26/A05
 XPIC                              Enabled
 Autorestore                       Unknown
Slot 6 - MMU2 H, RAU2 X 15/A01HP
 XPIC                              Disabled

I Got the following using the Peasant awk

rl 2 xpicrecovery 5 5
rl 3 xpicrecovery 5 5
rl 4 xpicrecovery 5 5
rl 5 xpicrecovery 5 5

That's no good, since slot 2 is already enabled ( Autorestore Enabled)

I have to create command only whenr XPIC is enabled and Autorestore is NOT enabled :frowning:

Rudic, with your awk, I got no output...
Please help, I have to deliver my script asap :frowning:

Lucas

"asap" is a term highly deprecated in these forums...

With either script, I get

rl 3 xpicrecovery 5 5
rl 4 xpicrecovery 5 5
rl 5 xpicrecovery 5 5

We need more to start analysing. Is it possible your input has quirks? Show your OS and awk versions, commands / scripts, input data (maybe a hexdump of a few lines?), and error messages. Print $1 and $2 of your input data.

1 Like

Hi Lord Spectre

Please, review how you have executed the code from post #4. Based on your input it should have given you the desired output instead of no output as you mentioned.

Here's another iteration

awk '
$1 == "Slot"  { slot = $2     # I am in record that starts the block
                next }        # move on to next line
$1 == "XPIC"  { Xstatus = $2  # I am in record that contains XPIC
                next }        # nothing else, move on to read next line
$1 == "Autorestore" &&        # I am in record that ends block
   Xstatus == "Enabled" &&    # do I need to print?
   $2 != "Enabled" { print "rl", slot, "xpicrecovery 5 5" } # yes, I do print
' example.file
1 Like

RudiC, don't misunderstand me, when I wrote "asap" I didn't want to speed up people to reply, just to say that I have a problem to solve :slight_smile:
By the way, I solved the mistery, and you're right RudiC, my input has quirks, maybe because is coming from telnet, so I fixed it with:

dos2unix -a get.tmp > /dev/null 2>&1

Thank you all for collaboration, and for the many examples provided!!!

Lucas