LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-14-2012, 11:14 AM   #1
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Rep: Reputation: Disabled
how can i count # times string is repeated ?


Greg
Anne Murai
Anne Murai
John-Paul
David Conley
David Conley
Vasco da Gamma
adrianne
kevin
charles manus
Hugh Kenrick
Austin Dsouza
Austin Dsouza

how can i print the name and num of times it is used ?
 
Old 10-14-2012, 11:53 AM   #2
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
If you have the strings into a file called, for example, list.txt:

Code:
sort list.txt | uniq -c
 
Old 10-14-2012, 02:08 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 437

Rep: Reputation: 19
Or if you want the details of a single name from a long list, add a grep
Code:
~ $ cat >> list.txt
Greg
Anne Murai
Anne Murai
John-Paul
David Conley
David Conley
Vasco da Gamma
adrianne
kevin
charles manus
Hugh Kenrick
Austin Dsouza
Austin Dsouza
~ $ sort list.txt | uniq -c | grep -i david
      2 David Conley
~ $
 
Old 10-15-2012, 02:37 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
awk '{ a[$0]++ } END{ for (i in a) { print i ": " a[i] } }' infile.txt | sort

awk 'BEGIN{ PROCINFO["sorted_in"]="@ind_str_asc" } { a[$0]++ } END{ for (i in a) { print i ": " a[i] } }' infile.txt
This uses awk's associative arrays. Each array index is the line itself, and the value increments once each time the same line is encountered. Then at the end we just loop through the array and print the values.

In the first version, we pipe it through sort afterwards in order to get sorted output. If you're using gawk v.4+ you can use the second variation, as it has array sorting built in.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to define a string of repeated characters lynne007 Linux - Newbie 6 02-09-2011 04:52 AM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 08:22 PM
Getting the no repeated pairs out of a a string charlitos Programming 2 02-17-2009 03:52 AM
daemon to count number of times programs are being run dizzutch Linux - Software 0 10-27-2004 09:41 AM
find repeated characters in a string mcshen Programming 9 02-02-2004 06:43 PM


All times are GMT -5. The time now is 03:53 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration