The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
using awk to count no of records based on conditions aemunathan Shell Programming and Scripting 13 06-14-2009 07:41 AM
exclude records with null fields praveenK_Dudala Shell Programming and Scripting 2 03-01-2009 09:54 PM
Count records in a zip file tekster757 UNIX for Dummies Questions & Answers 3 09-24-2008 04:19 PM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 01:15 PM
finding null records in data file dsravan Shell Programming and Scripting 3 01-09-2007 03:46 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-02-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Awk to Count Records with not null

Hi,

I have a pipe seperated file
I want to write a code to display count of lines that have 20th field not null.


Code:
nawk -F"|" '{if ($20!="") print NR,$20}' xyz..txt

This displays records with 20th field also null.

I would like output as:

Quote:
2000 (number of lines with not null 20th field)
  #2 (permalink)  
Old 11-02-2009
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 761

Code:
nawk -F'|' '$20!="" {tot++} END{print tot}' infile

  #3 (permalink)  
Old 11-02-2009
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,134

Code:
awk -F"|" '$20 { NN++; next } { N++ } END { print "Null: " N "   Not Null:" NN }' file1

Bits Awarded / Charged to scottn for this Post
Date User Comment Amount
11-02-2009 Anonymous N/A 100
  #4 (permalink)  
Old 11-02-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Quote:
Originally Posted by scottn View Post
Code:
awk -F"|" '$20 { NN++; next } { N++ } END { print "Null: " N "   Not Null:" NN }' file1

Thanks, this works fine.
Sir can you please explain this.
  #5 (permalink)  
Old 4 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,134

Code:
awk -F"|"                                     # Field separator is | 
'
  $20 { NN++; next }                          # if $20 is defined (not null) add 1 to NN (not null) variable, and go to next record
                                              # (after a "next" no rules are evaluated, we go back to the start, for the next record)
  { N++ }                                     # We only get here is record 20 is not defined (is null) so add 1 to N (is null) variable
  END { print "Null: " N "   Not Null:" NN }  # finally, print both counts
' file1

Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:32 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0