Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google site



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-24-2008
Registered User
 

Join Date: Sep 2008
Posts: 21
Question Perl/shell script count the lines

Hi Guys,

I want to write a perl/shell script do parse the following file

input file content
NPA-NXX SC
2084549 45
2084552 45
2084563 2007
2084572 45
2084580 45
3278411 45
3278430 45
3278493 530
3278507 530
3278508 44
3278514 44



the output i require should contain first columns first three digits and then the count of numbers with that first three digits per SC wise

NPA Count(NPA) SC
208 6 45
208 1 2007
327 2 530
327 2 44


Thanks and regards
Sponsored Links
  #2 (permalink)  
Old 09-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652

Code:
sed 's/.... / /' file | sort | uniq -c

The output puts the counts before the entries; you can pass that to another simple sed or awk script if you really require the count to be the second field.
  #3 (permalink)  
Old 09-24-2008
Registered User
 

Join Date: Sep 2008
Posts: 21
Talking

thanks
  #4 (permalink)  
Old 09-24-2008
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,220
With AWK, only if you are curious

(use nawk or /usr/xpg4/bin/awk on Solaris):


Code:
awk 'END {
  for (k in _) {
    split(k, t, SUBSEP)
    print t[1], _[k], t[2]
    }
  }
NR == 1 { print; next }
{ _[substr($1,1,3), $2]++ }
' filename

Sponsored Links
Closed Thread

Bookmarks

Tags
count

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script to manipulate a message count for the same IP @ diff session gobinath Shell Programming and Scripting 1 05-28-2008 04:13 PM
Number of lines in a file (perl script) jisha Shell Programming and Scripting 5 05-20-2008 08:11 AM
Perl script to scan back lines gholdbhurg Shell Programming and Scripting 3 03-18-2008 12:33 PM
How to Run a shell script from Perl script in Parent shell? hifake Shell Programming and Scripting 16 08-28-2007 09:42 PM
How to count lines - ignoring blank lines and commented lines kthatch UNIX for Dummies Questions & Answers 6 05-25-2007 02:21 AM



All times are GMT -4. The time now is 10:23 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-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0