Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

grep and calculating # of entries

SOLVED
Go to solution
Highlighted
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
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