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
awk - Grouping lines dhanamurthy Shell Programming and Scripting 2 05-15-2008 09:47 PM
script to change filename with numbers silversurfer202 Shell Programming and Scripting 5 11-10-2006 10:13 AM
Need to total numbers at end of script wereyou Shell Programming and Scripting 9 09-22-2006 08:47 PM
add numbers in shell script Terrible Shell Programming and Scripting 2 07-25-2006 09:36 AM
egrep and grouping effigy Shell Programming and Scripting 1 10-14-2004 11:45 AM

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 11-01-2007
cdfd123 cdfd123 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 112
grouping of numbers with script

Suppose u have a file
2
4
6
11
22
13
23
43
12
4
33
31
45
then u want a output like
0-10 4
10-20 3
20-30 2
30-40 2
40-50 2
  #2 (permalink)  
Old 11-01-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
nawk -f cdcf.awk myFile

cdcf.awk:

Code:
{
  a[int($1/10)]++
}
END {
  for (i in a)
    printf("[%d-%d] %d\n", i*10, i*10+10, a[i])
}

  #3 (permalink)  
Old 11-01-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
A small addition to sort the output:


Code:
{
              a[int($1 / 10)]++
              if (int($1 / 10) > c) { 
                       c = int($1 / 10)
                }
        }

        END {
            for (i = 0; i <= c; i++) {
                    if (i in a) { 
                               printf "[%d-%d] %d\n", i * 10, i * 10 + 10, a[i]
                        }
                }
        }


Last edited by radoulov; 11-01-2007 at 07:29 PM.. Reason: corrected
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 04:22 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