UNIX gurus. need help

Discussion in 'OT Technology' started by NEOxMorpheus, Jan 26, 2006.

  1. NEOxMorpheus

    NEOxMorpheus Real OG

    Joined:
    Oct 24, 2001
    Messages:
    1,001
    Likes Received:
    0
    Got this problem that I need help on. Lets say you have a file called Hi.txt and that file has a name (ie David, Bob, David) on each line. What can you type on the command line that will display the count of how many times each name has appeared and also the name.

    ie:
    name count
    David 2
    Bob 1
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    You'd have to write a little script/program I think.

    However, if you know the name and you want to get the count, you can do:

    grep 'David.*' <file> |wc
     
  3. NEOxMorpheus

    NEOxMorpheus Real OG

    Joined:
    Oct 24, 2001
    Messages:
    1,001
    Likes Received:
    0
    What scripting languages can I use?
     
  4. sam758

    sam758 OT Supporter

    Joined:
    Aug 26, 2003
    Messages:
    901
    Likes Received:
    0
    you could use perl...

    Code:
    open(HANDLE, "test.txt");
    while(<HANDLE>){
        $_=~s/\s+$//;
        if(defined($namecheck{$_})){
            $namecheck{$_} += 1;
        }
        else{
            $namecheck{$_} = 1;
        }
    }
    foreach $key(keys %namecheck){
        print "$key: $namecheck{$key} times\n";
    }
    
    isuckatperl but it should probably work.:x:
     
    Last edited: Jan 26, 2006
  5. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,446
    Likes Received:
    12
    If you don't know what names are in that file then you can do this in your shell:

    for i in `cat Hi.txt | sort | uniq`
    do
    echo "$i appears `grep $i Hi.txt | wc -l` times in Hi.txt"
    done
     
  6. NEOxMorpheus

    NEOxMorpheus Real OG

    Joined:
    Oct 24, 2001
    Messages:
    1,001
    Likes Received:
    0
    thanks for the help. you can write for loops in the command console?
     
  7. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,446
    Likes Received:
    12
    you can script almost anything you want with file manipulation.
     
  8. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    :bowdown:

    I'm impressed.
     
  9. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    cron has some wicked *nix knowledge. makes 99.9% of us look like amatures.
     
  10. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Yep, he's like the labadmins at school. They type out these crazy long sequences of pipes and I'm like :eek4:
     
  11. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    i know. god damn im jealous.
     
  12. jackmacmoe

    jackmacmoe New Member

    Joined:
    Jan 26, 2006
    Messages:
    3
    Likes Received:
    0
    wow great :bowdown:
     

Share This Page