Solved

grep with regular expression

Posted on 2012-04-11
9
470 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now