Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

grep with regular expression

Posted on 2012-04-11
9
Medium Priority
?
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 1000 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

705 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