LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2010, 03:16 AM   #1
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Rep: Reputation: 0
bash calculate repeated lines


Dear all,

i have a sorted file with many repeated lines like this:
1ujw
1ujw
1ul1
1us7
1us7
1us7
1us7
1us7
1uuz
1uuz
1uuz
1uuz
1uuz
1uuz
1uuz
1uzx
1uzx
1uzx

i need to calculated haw many times each line is repeated, for ex
1ujw 2
1ul1 1
1us7 5
etc.


many thanks
 
Old 01-28-2010, 03:22 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
uniq -c file
 
1 members found this post helpful.
Old 01-28-2010, 03:27 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Something like this?

awk '{count[$1]++}END{for(j in count) print j,"("count[j]")"}' infile

Output of testrun:
Code:
$ awk '{count[$1]++}END{for(j in count) print j,"("count[j]")"}' infile
1ujw (2)
1uuz (7)
1uzx (3)
1us7 (5)
1ul1 (1)
Hope this helps.
 
1 members found this post helpful.
Old 01-28-2010, 03:29 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@ghostdog74: That's an option (uniq -c) I did not know about yet.... Nice short solution!
 
Old 01-28-2010, 03:38 AM   #5
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
Code:
uniq -c file

thank you very much. very elegant way!!!
 
Old 01-28-2010, 04:27 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
if you use uniq you need to sort first.

edit:
d'oh I see you have!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calculate logarithm in bash script bluesmoke Programming 5 12-14-2008 01:41 AM
bash calculate job duration Padawan.AVT Programming 2 10-07-2008 02:33 AM
how command is repeated in terminals in bash shell sagsriv Linux - Newbie 4 03-17-2008 03:53 AM
Bash Script, calculate output. eldaria Programming 13 07-20-2006 09:26 PM
Bash script - repeated command satimis Programming 27 10-28-2004 05:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration