grep with regular expression

Posted on 2012-04-11
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   ??
Question by:fosiul01
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
LVL 84

Accepted Solution

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)/'
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'
LVL 84

Expert Comment

ID: 37832439
As a single egrep expression, it would be
egrep 'Com_insert($|[^_]|_($|[^s])|_s($|[^e])|_se($|[^l])|_sel($|[^e])|_sele($|[^c])|_selec($|[^t]))'
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

LVL 84

Expert Comment

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
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).
LVL 84

Expert Comment

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'
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"'
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
LVL 29

Author Comment

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}'

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

623 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