LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 10: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, 10:53 AM   #2
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

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

Code:
sort list.txt | uniq -c
 
Old 10-14-2012, 01:08 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
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, 01:37 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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



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 03: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 07:22 PM
Getting the no repeated pairs out of a a string charlitos Programming 2 02-17-2009 02:52 AM
daemon to count number of times programs are being run dizzutch Linux - Software 0 10-27-2004 08:41 AM
find repeated characters in a string mcshen Programming 9 02-02-2004 05:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:01 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
Open Source Consulting | Domain Registration