Visit Our UNIX and Linux User Community


Sort and count using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort and count using AWK
# 1  
Old 10-14-2009
Sort and count using AWK

Hi,

I've a fixed width file where I need to count the number of patterns from each line between characters 1 to 15 . so can we sort them and get a count for each pattern on the file between 1 to 15 characters.



Code:
65795648617522383763831552  410828003265795648  6175223837
82255637216297152582255637  216297152565795648  6175223837                
61037863301869758061037863  301869758065795648  6175223837          
638315524108280032 63831552  4108280032         
617522383763831552  410828003265795648  6175223837
326579564863831552  4108280032 63831552  4108280032
3301869758061037863  301869758065795648  6175223837


Last edited by vgersh99; 10-14-2009 at 01:58 PM.. Reason: fixed code tags
# 2  
Old 10-14-2009
Code:
cut -c1-15 < myFile |sort -n | uniq -c

# 3  
Old 10-14-2009
Code:
awk '{a[substr($0,1,15)]++}END{for(i in a) print a[i],i | "sort -nk2"}' file

# 4  
Old 10-14-2009
Quote:
Originally Posted by vgersh99
Code:
cut -c1-15 < myFile |sort -n | uniq -c

This can be written with one less pipe and process: -

Code:
cut -c1-15 < infile |sort -n -u

OUTPUT: -

Code:
326579564863831
330186975806103
610378633018697
617522383763831
638315524108280
657956486175223
822556372162971

# 5  
Old 10-14-2009
Quote:
Originally Posted by steadyonabix
This can be written with one less pipe and process: -
... and where you count ? did you check the OP request?
# 6  
Old 10-14-2009
Quote:
Originally Posted by danmero
... and where you count ? did you check the OP request?
Smilie

Nope, not been that sharp today, my bad.......

Previous Thread | Next Thread
Test Your Knowledge in Computers #931
Difficulty: Medium
64-bit computer system storage allocation for timekeeping will allow them to represent dates more than 300 billion years into the future.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Sort words based on word count on each line

Hi Folks :) I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line. Example from: word word word word word word word word word word word word word word word word to desired output: word (2 Replies)
Discussion started by: martinsmith
2 Replies

2. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

3. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

4. Shell Programming and Scripting

Advanced: Sort, count data in column, append file name

Hi. I am not sure the title gives an optimal description of what I want to do. Also, I tried to post this in the "UNIX for Dummies Questions & Answers", but it seems no-one was able to help out. I have several text files that contain data in many columns. All the files are organized the same... (14 Replies)
Discussion started by: JamesT
14 Replies

5. Shell Programming and Scripting

count characters in multiple files and sort

I am trying to count a special character in several thousand files and sort the result according to the number the character occured in each files. files: 1.txt 2.txt 3.txt files look like: ABCDEFGHABBBACF I want the number of B in each file and a result file that lists the occurence... (4 Replies)
Discussion started by: patdoor
4 Replies

6. Shell Programming and Scripting

Perl - Sort and Count foreach line

Can any one please help I am trying to sort each item in every line and count the common (non case sensitive) and at the end printing all the unique alphabetically. Here is what i did ... I can print all the lines but struck to sort each line some were: I am getting the output as: I... (5 Replies)
Discussion started by: sureshcisco
5 Replies

7. Shell Programming and Scripting

Beginner: Count & Sort Using Array's

Hi, I'm new to linux & bash so please forgive my ignorance, just wondering if anyone can help. I have a file (mainfile.txt) with comma deliminated values, like so: $1 $2 $3 613212, 36, 57 613212, 36, 10 613212, 36, 10 677774, 36, 57 619900, 10, 10 i need to split this file... (12 Replies)
Discussion started by: BigTOE
12 Replies

8. Shell Programming and Scripting

awk: sort lines by count of a character or string in a line

I want to sort lines by how many times a string occurs in each line (the most times first). I know how to do this in two passes (add a count field in the first pass then sort on it in the second pass). However, can it be done more optimally with a single AWK command? My AWK has improved... (11 Replies)
Discussion started by: Michael Stora
11 Replies

9. UNIX for Dummies Questions & Answers

Sort with respect to count

Hello! I have a file with 4 columns. I am trying to have it sort first with respect to the first column, and then with respect to the number of counts (in descending count) in the second column within the same first column identity. For example: Input: 1 2 A 1 1 6 B 2 2 5 G 7 1 6 D 4... (8 Replies)
Discussion started by: anchuz
8 Replies

10. Shell Programming and Scripting

How to sort by count

Hello! I have a file with 4 columns. I am trying to have it sort first with respect to the first column, and then with respect to the number of counts (in descending count) in the second column within the same first column identity. For example: Input: 1 2 A 1 1 6 B 2 2 5 G 7 1 6 D 4 1... (1 Reply)
Discussion started by: anchuz
1 Replies

Featured Tech Videos