Solved

AWK: number of lines returned ?

Posted on 2007-11-20
5
1,035 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

Gigs: Get Your Project Delivered by an Expert

Select from 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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…
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…

785 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