Solved

AWK: number of lines returned ?

Posted on 2007-11-20
5
1,040 Views
Last Modified: 2013-12-26
How do you get the number of lines returned by an
AWK search, that is the number of matches that it
has found ?
0
Comment
Question by:pillmill
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 20322494
awk | wc -l
0
 

Author Comment

by:pillmill
ID: 20322598
Thanks.
Can I assign this result to a variable ?
(I'm using Bourne shell)

I want to test if there is more than match found.
For example,

f= [ awk | wc -l  ]

if f > 2 then ...
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20322683
Why awk and not grep?

if [ `grep -c string file` -gt 2 ]
then
    echo "More than 2 matches"
fi
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 20326642
f=`awk '/your pattern match/{l++}END{print l}' yourfile`
f=`grep -c "your pattern match" yourfile`

awk '(l>2){print "too much"}/your pattern match/{l++}' yourfile
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

685 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