Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

grep and calculating # of entries

SOLVED
Go to Solution
Highlighted
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
Honored Contributor Honored Contributor

Re: grep and calculating # of entries

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

Re: grep and calculating # of entries

grep -e vgcreate -e createVG
Honored Contributor Honored Contributor

Re: grep and calculating # of entries

Hi

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

Steve
take your time and think things through
Acclaimed Contributor 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...
Valued Contributor 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-


Frequent Advisor Frequent Advisor

Re: grep and calculating # of entries

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