Solved

grep with regular expression

Posted on 2012-04-11
9
478 Views
Last Modified: 2012-04-12
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   ??
0
Comment
Question by:fosiul01
  • 4
  • 4
9 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 37832391
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)/'
0
 
LVL 23

Assisted Solution

by:Maciej S
Maciej S earned 250 total points
ID: 37832419
use -w option for grep:

/opt/mysql/server-5.5/bin/mysql -S /tmp/mysql.sock -e ' SHOW GLOBAL STATUS  ' |  grep -w 'Com_insert'
0
 
LVL 84

Expert Comment

by:ozo
ID: 37832439
As a single egrep expression, it would be
egrep 'Com_insert($|[^_]|_($|[^s])|_s($|[^e])|_se($|[^l])|_sel($|[^e])|_sele($|[^c])|_selec($|[^t]))'
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 84

Expert Comment

by:ozo
ID: 37832459
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
0
 
LVL 23

Expert Comment

by:Maciej S
ID: 37832489
I agree - it's not equivalent, but it's an answer for fosiul's question (with his output).
0
 
LVL 84

Expert Comment

by:ozo
ID: 37832541
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'
0
 
LVL 23

Expert Comment

by:Maciej S
ID: 37832665
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"'
0
 
LVL 23

Expert Comment

by:Maciej S
ID: 37832672
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
0
 
LVL 29

Author Comment

by:fosiul01
ID: 37833002
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}'
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question