grep and calculating # of entries (264 Views)
Reply
Frequent Advisor
andi_1
Posts: 98
Registered: ‎03-04-2002
Message 1 of 7 (264 Views)
Accepted Solution

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!
Honored Contributor
Mladen Despic
Posts: 413
Registered: ‎02-19-2001
Message 2 of 7 (264 Views)

Re: grep and calculating # of entries

grep -E 'vgcreate|createVG'
Honored Contributor
Mladen Despic
Posts: 413
Registered: ‎02-19-2001
Message 3 of 7 (264 Views)

Re: grep and calculating # of entries

grep -e vgcreate -e createVG
Honored Contributor
steven Burgess_2
Posts: 1,493
Registered: ‎11-21-2001
Message 4 of 7 (264 Views)

Re: grep and calculating # of entries

Hi

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

Steve
take your time and think things through
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 7 (264 Views)

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
David Totsch
Posts: 44
Registered: ‎02-07-1999
Message 6 of 7 (264 Views)

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
Jack Werner
Posts: 70
Registered: ‎04-02-2001
Message 7 of 7 (264 Views)

Re: grep and calculating # of entries

grep -c -e patt1 -e patt2
i'm retired
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.