using awk instead of wc -l

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

using awk instead of wc -l

Hi,

 

I am wanting to use awk to count a nuimber of lines where the /match statment is true:

 

# cat file | awk -F':' '/^FDC-6/ && !/VMM/ {if($6 !~ /\-id/) print}' |wc -l
      56

 

I want to know how I can count the lines using somethuing like i++ but struggling to get the syntax to work

 

# cat file | awk -F':' '/^FDC-6/ && !/VMM/; BEGIN{i=0};{if($6 !~ /-id/ then i++)};END{print i}'

 

but I know this is incorrect

 

any help much appreciated

 

Chris.

 

 

hello
2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: using awk instead of wc -l

>I want to know how I can count the lines using something like i++ but struggling to get the syntax to work

 

Use separate lines to see it better:
awk -F':' '

BEGIN {i=0}

/^FDC-6/ && !/VMM/ {

   if($6 !~ /-id/) i++

}

END{print i}' file

 

Which can be improved by combining your conditions:

/^FDC-6/ && !/VMM/ && ($6 !~ /-id/)  { i++ }

Sachin Rajput
Advisor

Re: using awk instead of wc -l

A simple grep and wc -l will do it if you look for a easy method .

cat filename | grep -i "yourtexttomatch" |grep -i "second_contidion" wc -l

Second thing using awk example :
#### Awk without condition for one match .
$ cat /tmp/sktest1|awk 'BEGIN {c=0} /Creating/ {c++ ;print c,$2,$3}'
1 Creating System
2 Creating Map
3 Creating Map

####### Awk with one condition and one match .

$ cat /tmp/sktest1|awk 'BEGIN {c=0} /Creating/ {if($3=="Map")c++ ;print c,$2,$3}'
0 Creating System
1 Creating Map
2 Creating Map


### Awk with two matches AND operation .
$ cat /tmp/sktest1|awk 'BEGIN {c=0} /Creating/ && /Map/ {c++ ;print c,$2,$3}'
1 Creating Map
2 Creating Map


Like this you can use any combination .

Hope this helps .

Sachin Rajput
=================


Sachin Rajput
================