Results 1 to 4 of 4
I need to count the ammout of different names that are given by an application in this form:
Name1
Name2
Name3
.......
now I could use:
Code:
for online in ...
- 10-15-2008 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 10
Counting names
I need to count the ammout of different names that are given by an application in this form:
Name1
Name2
Name3
.......
now I could use:
however if there was twice John it would get counted as two and not one.Code:for online in `$NAMESBIN | tr -d ' '`; do USERSON="$( expr "$USERSON" \+ "1" )" done
So basically:
John
Britney
David
Paul
John
would result 5 while it's only 4 different names.
How to make it properly ?
- 10-15-2008 #2
You'll need to do something to keep track of the names you've already recorded. Depending on how you do this, it could be memory intensive. For example, you could just look back down the list for every name you've counted. You could sort them alphabetically as you go, making it easier to find and verify whether a name has already been used. Or you could use some sort of hash table structure to assign names as keys to some value and then just count the keys.
Registered Linux User: #479567
Asking a question? Read this page first.
Now... sudo make me a sandwich.
ratiocinativeroot.blogspot.com
- 10-15-2008 #3Just Joined!
- Join Date
- Oct 2008
- Posts
- 10
Someone has suggesting using sort -u filename | wc -l and then counting but I'd need it done in memory and not file... any ideas?
- 10-16-2008 #4Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
1. type "man sort"
2. read carefully
3. discover the following statement: "With no FILE, or when FILE is -, read standard input."
4. read Filter (Unix - Wikipedia, the free encyclopedia) or similar


Reply With Quote