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
help wanted kernel avi iuno UNIX for Advanced & Expert Users 2 03-27-2004 01:15 PM
Suggestions wanted ... Cameron UNIX for Dummies Questions & Answers 3 02-19-2003 10:12 PM
Help wanted reddyb High Level Programming 3 08-28-2001 12:49 PM

Closed Thread
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 12-12-2007
carl_r carl_r is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 1
printing only wanted rows in awk

Hi!
The fallowing awk script counts words from input file, then sorts these words to decreasing order of occurrences and also to alphabetical order. And then prints all these words out with the number of their occurrence. For example:

and 7
for 4
make 4
you 4
awk 1
....

Problem is that if the text file includes thousands of words then the output is also very long. And I'm only interested of first 10 most occurred word, which means that I'd like to print out only first 10 rows. I have tried to change the printf command to print only first 10 sorted rows, but i have had no success Is it even possible to achieve this goal by only changing the printf command? Should i try something else?

script:

{
$0 = tolower($0)
gsub(/[^[:alnum:]_[:blank:]]/, "", $0)
for (i = 1; i <= NF; i++)
freq[$i]++
}

END {
sort = "sort -k 2nr"
for (word in freq)
printf "%s\t%d\n", word, freq[word] | sort
close(sort)
}



Thanks in advance!
  #2 (permalink)  
Old 12-12-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Have you considered "head"?


Code:
man head

  #3 (permalink)  
Old 12-12-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by porter View Post
Have you considered "head"?


Code:
man head
nicely put, porter!
[sorry, I could not resist!]
  #4 (permalink)  
Old 12-12-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
sort = "sort -k 2nr | head -10"

But why are you sorting inside awk?
Would not it be better to 'post=process' the manimulated data AFTER?

Code:
nawk -f myAWKscriptWithOUTsorting.awk my dataFile | sort -k 2nr | head -10

Closed Thread

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 05:08 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