Operating System - Linux
1753823 Members
9196 Online
108805 Solutions
New Discussion юеВ

counting number of rows in awk

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

counting number of rows in awk

hello all,

I am now attempting to add up the number of rows in the file that have more than 42 characters and output the total number of rows:

nawk '{len = length($0); if (len > 42) ??

I have attempted some syntax that I use to count total rows in the file however how can I just count the rows > 42?

Thanks

Chris.
hello
5 REPLIES 5
Sandman!
Honored Contributor
Solution

Re: counting number of rows in awk

# nawk '{if(length($0)>42) cnt++}END{print cnt}' file
James R. Ferguson
Acclaimed Contributor

Re: counting number of rows in awk

Hi Chris:

# awk 'END{print i};{len=length($0);if (len > 42) {i++}}'

Regards!

...JRF...
Oviwan
Honored Contributor

Re: counting number of rows in awk

Hey

use sed:
sed -n '/^.\{43\}/p' file1 | wc -l

Regards
James R. Ferguson
Acclaimed Contributor

Re: counting number of rows in awk

Hi (again) Chris:

You can even write this more tersely, thusly:

# awk 'length>42 {i++};END{print i}' file

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: counting number of rows in awk

nice one

thanks again all!
hello