Sequential counting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sequential counting
# 1  
Old 01-07-2013
Sequential counting

Hi have a file of the following format
Code:
a1
a1
a2
a2
a4
a4
a4
a3
a3
a5
a6
a6
a6
a6
..

100's of lines

The file is sorted and I would like to get the counts of each of the characters
the output I needed
Code:
a1 2
a2 2
a4 3
a3 2
a5 1
a6 4

let me know the best way to do it in awk.
# 2  
Old 01-07-2013
Considering that the file is already ordered, you could use:

Code:
uniq -c infile

Otherwise:

Code:
sort infile | uniq -c

With awk it would be:

PHP Code:
awk '{ cnt[$0]++ }
END { 
  for (e in cnt)
    print cnt[e], e
    }' 
infile 
The awk script won't preserve the original order.
# 3  
Old 01-07-2013
for awk in order, try:
Code:
awk '!a[$0]++ {} END {for (i in a) print i, a[i]}' infile

ordered as listed:
Code:
awk '!a[$0]++ {o[n++]=$0} END {for (i=0; i<n ; i++) print o[i], a[o[i]]}' infile


Last edited by rdrtx1; 01-07-2013 at 01:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for sequential pattern

input file: 4 5 1 A 1 2 3 4 s 8 (input file can be many millions of lines long) I want to search the example input file above, and when I find 4 sequential rows with values of 1,2,3,4 return those values and the two previous ones. In this case it should return 1,A,1,2,3,4 I know... (8 Replies)
Discussion started by: cedenker
8 Replies

2. Shell Programming and Scripting

Extracting sequential pattern

Hi, Can someone advise/help me on how to write a script to extract sequential lines. I was able to find and get a script working to create permutations of the inputs, but that not what I want/need. awk 'function perm(p,s, i) { for(i=1;i<=n;i++) if(p==1) ... (4 Replies)
Discussion started by: fuzzi
4 Replies

3. Shell Programming and Scripting

Sequential numbering from 1 to ten

Hi I am in a bind, I need create a script that will rename files as they come into a folder with sequential numbering at the begining starting at 1 and proceeding to ten then starting at 1 again. Such as 1_filename.pdf, 2_filename.pdf, 3_filename.pdf, 4_filename.pdf, 5_filename.pdf, 6_filename.pdf,... (6 Replies)
Discussion started by: Paul Walker
6 Replies

4. Shell Programming and Scripting

Sequential Function Execution

I am having two different function in my script. When control is at first function I do not want to execute another function. How I can do that? Help is highly appreiated as I am not sure How I can do it in Unix? Thanks, Vikram. (2 Replies)
Discussion started by: VSom007
2 Replies

5. Shell Programming and Scripting

Sequential extraction of first row

I do have a large text file (tab delimited) of the following format RT_1 34 67 RT_1 9 10 RT_1 98 56 RT_2 09 89 RT_2 23 45 RT_2 90 76 RT_3 98 21 RT_4 23 90 RT_4 67 90 RT_4 79 23 RT_5 8 9 I want to parse every first row (complete row), whenever a new value in column 1 (here... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

6. Shell Programming and Scripting

Sequential numbers

Hi All, I am looking for a simple way to write numbers to a file sequentially starting from 1 and ending on a specified upper limit. Example of the output file is below Example 1 2 3 4 5 . . . . 1000 please let me know the best way to do it. (10 Replies)
Discussion started by: Lucky Ali
10 Replies

7. Programming

Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX

Writing a Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX I have over the years come across the same issue a couple of times, and it normally is that the read speed on SAN is absolutely atrocious when doing non-sequential I/O to the disks. Problem being of... (7 Replies)
Discussion started by: vrghost
7 Replies

8. Shell Programming and Scripting

sequential to line sequential

Hi I have a file sequential way i.e. written in contineous mode and the Record Seperator is AM from which the record is seperated .Now to process I have to make line sequential,and more over record length is not same it varies as per the input address, AM1234563 John Murray 24 Old streeet old... (5 Replies)
Discussion started by: vakharia Mahesh
5 Replies

9. Programming

Reading special characters while converting sequential file to line sequential

We have to convert a sequential file to a 80 char line sequential file (HP UX platform).The sequential file contains special characters. which after conversion of the file to line sequential are getting coverted into "new line" or "tab" and file is getting distorted. Is there any way to read these... (2 Replies)
Discussion started by: Rajeshsu
2 Replies

10. Shell Programming and Scripting

running script sequential

I have 4 scripts I need to run sequentially(can't run simultaneously) What's the syntax for it? I am running Korn Shell. Thanks, (2 Replies)
Discussion started by: ocjunky
2 Replies
Login or Register to Ask a Question