-2
#!/bin/bash

who |grep "10\.1\.109" | grep -v berianho | cut -f1 -d " " | sort -n|

while read user 
do 
    grep -a ^$user: /etc/passwd | cut -f5 -d:
done

closed as unclear what you're asking by Jesse_b, muru, Kusalananda Jun 15 at 14:16

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • This is my code but it is not working. I want to print all lines uniqely with a number how many times is it repeated. – Student Jun 15 at 13:21
  • 2
    Please edit your question to define what you mean by "not working". What is the contents of who? What is your script returning? What would you like it to return? – Jesse_b Jun 15 at 13:22
  • 2
    It is not. You have nothing but a code block. You need to explain what you want and what it is doing wrong. You also should show us sample input and sample output. – Jesse_b Jun 15 at 13:45
  • 1
    You should also explain why you are doing grep "10\.1\.109". Why are you doing grep -v berianho? – Jesse_b Jun 15 at 13:49
  • 1
    Did you know that you can put non-code words in the question? Please read @jesse_b's comments, then use your words. – ctrl-alt-delor Jun 15 at 15:32
-1

This is my assumption on what you want to do.

#!/bin/bash
list="$(who |grep "10\.1\.109" | grep -v berianho | cut -f1 -d " ")"
unique="$(echo ${list} | tr ' ' '\n' | sort | uniq)"

for student in $unique
do
  echo "Student $(grep -a ^${student}: /etc/passwd | cut -f5 -d ":" ) has number of $(echo "$list" | tr ' ' '\n' | grep ${student} | wc -l) logins."
done

Example output will be assuming AAA (with name AAA AAA on passwd) has 3, BBB(with name BBB BBB on passwd) has and CC (with name CCC CCC on passwd) has 1 entries on who. the output must be:

Student AAA AAA has number of 3 logins.
Student BBB BBB has number of 2 logins.
Student CCC CCC has number of 1 logins.
  • 1
  • Thank you very much :) – Student Jun 15 at 13:49
  • Sorry, it says "Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. ********************************************************************* I forgot to say that im using PUTTY, maybe its a bit different. – Student Jun 15 at 14:26
  • Now that you have quoted $unique it will no longer expand in the way you initially intended. You will have to use an array instead of a variable. – Jesse_b Jun 15 at 14:36
  • 1
    (╯°□°)╯︵ ┻━┻. – Jesse_b Jun 15 at 15:10
0

A simple perl script ...

#!/usr/bin/perl

my %hash;

open FH, 'who |' or die;
while ( <FH> ) {
   $hash{$1}++ if /^(\S+).*(10\.\d+\.\d+\.\d+)/;
}
close FH;

while ( ($k,$v) = each %hash ) {
   printf "%3d %s\n", $v, $k;
}

exit;
  • 1
    Hello ingopingo. Could you please explain to the OP what this does and how this works, so that they (and others) can learn from you. (The question itself isn't clear, so it would probably be useful for you to clarify what you've understood from it, so that that OP can determine whether or not your answer is useful.) – roaima Jun 15 at 14:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.