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
need help in sort ali560045 Shell Programming and Scripting 2 12-04-2007 07:38 AM
sort prasathlogu UNIX for Dummies Questions & Answers 1 10-08-2007 07:56 AM
du -h | sort ? fongthai Shell Programming and Scripting 6 11-02-2006 08:59 PM
How do i sort? abhijeetkul Shell Programming and Scripting 1 12-22-2005 03:49 AM
2nd sort key whatisthis UNIX for Dummies Questions & Answers 1 10-20-2004 11:46 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 01-27-2008
mirusnet's Avatar
mirusnet mirusnet is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 146
sort

What is the easiest way to count duplicate lines?

example:
aaa
b
b
b
b
b

Output:
aaa 1
b 5


Sorry, maybe someone already ask for it,
but I can not find.
  #2 (permalink)  
Old 01-27-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
There can be some very simple solution to this problem, BUT using awk, this is how we can achieve this


Code:
$ cat file.txt
aaa
b
b
c
c
b
b

I am adding a second field to all lines of file.txt as "1", then using associative array.

$ awk '{print $0,1}' file.txt | awk '{arr[$1]+=$2} END {for (i in arr) {print i,arr[i]}}'
aaa 1
b 4
c 2
//Jadu
  #3 (permalink)  
Old 01-27-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
Quote:
Originally Posted by mirusnet View Post
What is the easiest way to count duplicate lines?

example:
aaa
b
b
b
b
b

Output:
aaa 1
b 5


Sorry, maybe someone already ask for it,
but I can not find.
A quick search of this site would have turned up lots of threads on related issues.

Code:
awk '{count[$1]++}END{for(j in count) print j,count[j]}'
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 05:43 PM.


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