Perl/shell script count the lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Perl/shell script count the lines
# 1  
Old 09-24-2008
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
# 2  
Old 09-24-2008
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  
Old 09-24-2008
Computer

thanks Smilie
# 4  
Old 09-24-2008
With AWK, only if you are curious Smilie

(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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux shell script to insert new lines based on delimiter count

The input file is a .dat file which is delimited by null (^@ in Linux). On a windows PC it looks something like this (numbers are masked with 1). https://i.imgur.com/nta2Gqp.jpg The entire file is in one row but it has multiple records - each record contains 80 fields i.e. there are 81 counts... (9 Replies)
Discussion started by: digitalnirvana
9 Replies

2. Shell Programming and Scripting

Shell script count lines and sum numbers from multiple files

I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines: It counts the number of lines: egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies

3. Shell Programming and Scripting

[perl script] print the assembly instruction and count the occurence

Hi, I have a input file(text file) with the following lines. 0x000000 0x5a80 0x0060 BRA.l 0x60 ;file:UserCall.s ;function:_user_call_table ;C_sourceLine:24 0x000002 0x1bc5 RETI ;file:UserCall.s ;function:_user_call_table ;C_sourceLine:30 0x000003 0x6840 MOV R0L,R0L ;file:UserCall.s... (6 Replies)
Discussion started by: acdc
6 Replies

4. Shell Programming and Scripting

Need perl or shell script to sort vertical lines to horizontal line in csv format

Need perl or shell script to sort vertical lines to horizontal line in csv format My file like below ------------------------- ================================================================================ PATH PINKY1000#I1-1-ZENTA1000-2#I7-1-ASON-SBR-UP-943113845 ... (4 Replies)
Discussion started by: sreedhargouda.h
4 Replies

5. Shell Programming and Scripting

count number of entries perl program or Unix script

Hi I have a file with number of entries name 1 123 name 1 345 name 1 65346 name2 3243 name2 24234 name 2 234234 so on ......... how to count total number of entries for name 1 and name2...and so on Please guide. (1 Reply)
Discussion started by: manigrover
1 Replies

6. Shell Programming and Scripting

perl count lines with certain word int

Hi Guys I have a text file that contains the message like this /var/log/messages.all-20120401: Mar 26 12:12:23 brent kernel: NVRM: Xid (0003:00): 43, 0005 00009097 00000000 00000000 00001b0c 1000f010 /var/log/messages.all-20120401: Mar 27 20:42:40 brent kernel: NVRM: Xid (0003:00): 43,... (4 Replies)
Discussion started by: ab52
4 Replies

7. Shell Programming and Scripting

Help with script to read lines from file and count values

Hi, I need some help with a script I'm trying to write. I have a log file containing references to a number of different webservices. I wish to write a script that will list the webservices with a count as to how many times they appear in the log. An example of the log file content: ... (2 Replies)
Discussion started by: gman2010
2 Replies

8. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

9. Shell Programming and Scripting

Count uncommented, blank and source lines in perl

Hi friends, I am working on a perl script to count the commented lines, blank lines and source lines separately. Let me know if you have one. For example i have a file containing the lines: /** * SOURCE CODE */ public class SessionConstants { /** * Code for Session created */... (4 Replies)
Discussion started by: nmattam
4 Replies

10. Shell Programming and Scripting

Modify a perl script to find and count

Hello all !I have two sets of folders that have IP address from two sources.The below perl script I was working with needs some corrections.I am looking for the perl script to identify and count what IP address are found to be duplicated between both files.The format from both files are the same... (4 Replies)
Discussion started by: richsark
4 Replies
Login or Register to Ask a Question