Operating System - HP-UX
1752614 Members
4441 Online
108788 Solutions
New Discussion юеВ

Re: grep and calculating # of entries

 
SOLVED
Go to solution
andi_1
Frequent Advisor

grep and calculating # of entries

Hi guys,

Sometime my file will contain the following entries:

vgcreate vg01
createVG vg01
createVG vg01

vgcreate vg02
createVG vg02

vgcreate vg03

I want to calculating # of occurences of vgcreate and createVG for every disk:
e.g.
tmp=`grep -e "^vg*.* $VGNAME" $CMD |wc -l`

The above grep searches only for vgcreate, does anyone know how can I add 'or' and also search for createVG?

Thanks guys!
6 REPLIES 6
Mladen Despic
Honored Contributor
Solution

Re: grep and calculating # of entries

grep -E 'vgcreate|createVG'
Mladen Despic
Honored Contributor

Re: grep and calculating # of entries

grep -e vgcreate -e createVG
steven Burgess_2
Honored Contributor

Re: grep and calculating # of entries

Hi

egrep '^(vg*|cr*)' | wc -l

Steve
take your time and think things through
James R. Ferguson
Acclaimed Contributor

Re: grep and calculating # of entries

Hi:

You can do:

# grep -e "^vg" -e "createVG"

...or make this case-insensitive too:

# grep -i -e "^fv -e "createVg"

# ...or:

# grep -i -E '^VGcreate|createvg'

Regards!

...JRF...
David Totsch
Valued Contributor

Re: grep and calculating # of entries

Put the -c option into your grep(1) and you can avoid calling wc(1) to do the counting work:

grep -cE "^(vgcreate|createVG) vg01"

But now you have to call it separately for each VG. I am lazy (really lazy), so I would go for making awk(1) count everything all at once: place this into a file (say "counter.awk"):

$1 ~ /^(vgcreate|createVG)/ {totals[$2]++}
END {for (VG in totals) print VG,totals[VG]}

Now, you can run it with

awk -f counter.awk data

and get a report that looks like:

vg01 3
vg02 2
vg03 1

-dlt-


Jack Werner
Frequent Advisor

Re: grep and calculating # of entries

grep -c -e patt1 -e patt2
i'm retired