grep with regular expression

mysql -S /tmp/mysql.sock -e ' SHOW GLOBAL STATUS  ' | grep 'Com_insert'
 which gives bellow result

Com_insert      353
Com_insert_select       0

but this

/opt/mysql/server-5.5/bin/mysql -S /tmp/mysql.sock -e ' SHOW GLOBAL STATUS  ' |  grep 'Com_insert' | grep -v 'Com_insert_select'

it gives
Com_insert      353
which is right..
but is there any clean way to write this command  so that
i only get 'Com_insert      353'

i meant : by using regular expression or regular expresson + awk   ??
LVL 29
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

A pure regular expression equivalent to  grep 'Com_insert' | grep -v 'Com_insert_select'  can get a little awkward to write.
It may be easier to use something like
perl -ne 'print if /Com_insert(?!_select)/'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maciej SsysadminCommented:
use -w option for grep:

/opt/mysql/server-5.5/bin/mysql -S /tmp/mysql.sock -e ' SHOW GLOBAL STATUS  ' |  grep -w 'Com_insert'
As a single egrep expression, it would be
egrep 'Com_insert($|[^_]|_($|[^s])|_s($|[^e])|_se($|[^l])|_sel($|[^e])|_sele($|[^c])|_selec($|[^t]))'
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

grep -w 'Com_insert'
is not equivalent to grep 'Com_insert' | grep -v 'Com_insert_select'
because grep 'Com_insert' | grep -v 'Com_insert_select' would match something like "Com_insertion"
whereas grep -w 'Com_insert' would not
Maciej SsysadminCommented:
I agree - it's not equivalent, but it's an answer for fosiul's question (with his output).
for the specific output listed in the question,
grep 'Com_insert' | awk 'NR==1'
would also be an answer,
so would
grep 'Com_insert     353'
Maciej SsysadminCommented:
I won't argue with this - your perl solution is the most general one - I agree.
But - for this specific question, where input for grep is "show global status" output - my version is simpler, and will not cause unwanted output. And yes - author can also run "grep 'Com_insert '" (with space at the end). IMO -w option is better than adding space. It's (still IMO) also better then your perl - it's simpler and shorter. If author wants to use it in future in some other cases, than your perl may be better (it depends on input for grep/perl).

One more solution - without any grep, awk, perl, etc:

/opt/mysql/server-5.5/bin/mysql -S /tmp/mysql.sock -e ' SHOW GLOBAL STATUS  where variable_name="Com_insert"'
Maciej SsysadminCommented:
Above will output headers also. The one with desired output only (with -B -N options for mysql):

/opt/mysql/server-5.5/bin/mysql -S /tmp/mysql.sock -e ' SHOW GLOBAL STATUS  where variable_name="Com_insert"' -BN
fosiul01Author Commented:
Hi thanks for variataion ...

just one more variation i want to add , this also gives the accurate result ..

/opt/mysql/server-5.5/bin/mysql -S /tmp/mysql.sock -e ' SHOW GLOBAL STATUS  ' | awk '$1~/^Com_insert$/ {print $2}'
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.