Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    10

    Question 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:

    Code:
    for online in `$NAMESBIN | tr -d ' '`; do
        USERSON="$( expr "$USERSON" \+ "1" )"
    done
    however if there was twice John it would get counted as two and not one.

    So basically:

    John
    Britney
    David
    Paul
    John

    would result 5 while it's only 4 different names.

    How to make it properly ?

  2. #2
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    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

  3. #3
    Just 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?

  4. #4
    Linux 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...