Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

what is the syntax to get the count of 2 or more strings..

Posted on 2014-12-10
2
Medium Priority
?
133 Views
Last Modified: 2014-12-13
Hi,
I want to grep a file for 2 or more strings and get a count back.

for one string I have this..
RETVAL=`egrep -c '(PLS)-' ${my_file}`

what is the syntax to get the count of 2 or more strings..

if my file has any of these strings return the count
file name = "my_file"
strings="PLS" and "ORA" and "ERR"

Thx, Bill
0
Comment
Question by:bcarlis
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 40492261
Do you mean the cumulative count?

RETVAL=$(egrep -c 'PLS|ORA|ERR' $my_file)
echo $RETVAL

If you have to check for the presence of the parentheses and the minus sign (as in your example):

RETVAL=$(egrep -c '\(ORA\)-|\(ERR\)-|\(PLS\)-' $my_file)
echo $RETVAL

To count multiple matches per line (GNU grep only):

RETVAL=$(egrep -o '\(ORA\)-|\(ERR\)-|\(PLS\)-' $my_file |wc -l)
echo $RETVAL
0
 
LVL 2

Author Closing Comment

by:bcarlis
ID: 40498483
Thank you!
RETVAL=$(egrep -c 'PLS|ORA|ERR' $my_file)
works great!
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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 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…
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
Course of the Month5 days, 22 hours left to enroll

772 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