Fetching the required data out of a tabular form

Hello Gurus,

I am trying to fetch a required number of lines from an output of a command which is in tabular form.

Below is the command for reference along with how the result is being shown on UNIX shell.

/usr/openv/volmgr/bin/vmquery -b -p 5

The result of the above command is as below.

media   media  robot  robot  robot  side/  optical  # mounts/      last
 ID     type   type     #    slot   face   partner  cleanings    mount time
A01202  HCART  TLD      0     257     -       -          15     03/27/2016 07:29
A01262  HCART  TLD      0     361     -       -          31     05/20/2016 13:02
A01282  HCART  TLD      0      51     -       -          34     05/20/2016 15:21
KM3440  HCART  TLD      1      62     -       -         492     05/12/2016 00:55
KM3443  HCART  NONE     -      -     -       -         306     03/14/2014 16:37
KM3458  HCART  TLD      1     109     -       -         362     05/12/2016 05:40
KM3468  HCART  NONE     -      -     -       -           2     06/25/2015 15:21
KM3509  HCART  NONE     -      -     -       -          87     09/09/2014 06:01
LJY535  HCART  TLD      1     180     -       -         197     05/12/2016 11:48

If you see at the output closely, there is a column name "robot#" with values as [0,1,-]. All I want to do is to grep all rows with "robot# 0" and get the total count for it with "wc -l".

Kindly help.

Regards

It seems like:

/usr/openv/volmgr/bin/vmquery -b -p 5 | awk '$4==0{c++}END{print c}'

should give you what you want.

If you are running this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

1 Like

Hello Ali Sarwar,

Could you please try following and let me know if this helps you.

/usr/openv/volmgr/bin/vmquery -b -p 5 | awk '{n+=gsub(/^0$/,X,$4)} END{print n}' 

I hope this helps.

Thanks,
R. Singh

Hello Ravinder & DON,

Don's command is exactly what I was looking for. Would appreciate if this can be explained a bit further that What C++ is used for ? Why $4==0 what does END being used for ? Thanks you.

/usr/openv/volmgr/bin/vmquery -b -p 5 | awk '$4==0{c++}END{print c}'

Regards,
A.S

Hello Ali Sarwar,

Following may help you in same.

awk '$4==0      ##### It is a condition check(similar what we do in if else condition check). Where $4 means 4th field and we are checking here if 4th field is 0 or not.
{
c++             ##### If 4th field is having ZERO value then there is variable named C, so we are incrementing it's value by doing c++ to one.
}
END{            ##### Here starts END block, in awk there are 3 sections, i- BEGIN- if you want to perform some operations in very starting of your program. ii- Body- where we write actual logic/code for our requirements. iii- END- block code will be executed when all Inputs are read from Input_file. 
print c}'       ##### printing the value of variable c, which will tell us how many 4th fields have value 0 in them.
 

Thanks,
R. Singh

1 Like

That is great. Thank you very much. I miss-understood C++ with the tool C++.. So its just that I can use any variable to put in the value and increment it till I find my results.

Worked perfectly how I needed it. Thank you both. I will now close this thread.

Regards,
A.s

---------- Post updated at 03:18 PM ---------- Previous update was at 03:10 PM ----------

Solved

Your Welcome Ali, for making threads solved. You should navigate to right most up corner of the page and click on EDIT TAGS and add solve there, in order to make a thread as solved. I will do it for you on this thread, you could explore that option though.

Thanks,
R. Singh