-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
8
  • 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 '19 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 '19 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 '19 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 '19 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 '19 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.
7
  • 1
  • Thank you very much :) – Student Jun 15 '19 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 '19 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 '19 at 14:36
  • 1
    (╯°□°)╯︵ ┻━┻. – jesse_b Jun 15 '19 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
  • 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 '19 at 14:18

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