Register
It is currently Sun Apr 22, 2018 9:50 pm

Bash Text Processing Help


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Aug 16, 2017 1:22 pm   

Joined: Wed Aug 16, 2017 1:19 pm
Posts: 2
I am able to retrieve below info from F5 and write to a file:

monitor-status up node-name servers-00-us
monitor-status up node-name servers-01-us
monitor-status down node-name servers-04-us
monitor-status down node-name servers-05-us
monitor-status up node-name servers-00-uk
monitor-status up node-name servers-01-uk
monitor-status down node-name servers-04-uk
monitor-status down node-name servers-05-uk

What I want to do with this file is read every line and if there is a match for 'up', add the servers-* to running list and if match for down then add to shutdown list.

The list name should be in [] and should take it's name from the first part before -.

So I would like to have is another file generated which looks like:

[servers-running]
servers-00-us
servers-01-us
servers-00-uk
servers-01-uk


[servers-shutdown]
servers-04-us
servers-05-us
servers-04-uk
servers-05-uk


Top
 Profile  
 PostPosted: Wed Aug 16, 2017 5:27 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Code:
awk '{if ($2=="down") down[$4]
      else   up[$4]
     }
     END {
        print "[servers-running]"
        for (s in up) print s
        print "[servers-shutdown]"
        for (s in down) print s
       }
    ' YourDataFileOrReadDataFromAPipeInsteadOfFileNameHere


Top
 Profile  
 PostPosted: Wed Aug 16, 2017 8:26 pm   

Joined: Wed Aug 16, 2017 1:19 pm
Posts: 2
Awesome, I will try it out. But just to clarify if I have below hosts in addition to ones starting with 'servers':

monitor-status up node-name hosts-00-us
monitor-status down node-name hosts-00-uk

I would like to see a separate grouping with:

[hosts-running]
hosts-00-us
[hosts-shutdown]
hosts-00-uk

in addition to below grouping:


[servers-running]
servers-00-us
servers-01-us
servers-00-uk
servers-01-uk


[servers-shutdown]
servers-04-us
servers-05-us
servers-04-uk
servers-05-uk


Top
 Profile  
 PostPosted: Sat Aug 19, 2017 9:22 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Code:
#!/bin/bash

awk '{split($4,g,"-"); print g[1],$2,$4}' data                            \
    | sort                                                                \
    | awk '{ if (hostgroup != $1 || status != $2 ) {
                    hostgroup = $1
                    status = $2
                    print "[" $1 "-" $2 "]"
                }
             print $3
           }
          '
Using a pipe for easier parsing and sorting.
The first awk reads the data file named data.
Mind the backslash at end of lines one and two.
Line continuing character must be the very last character of line.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 26 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP