-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
ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102
Student
  • 1
  • 4
  • 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](https://unix.stackexchange.com/posts/525119/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
  • I just edited the code, i want to print the output of that script such that print all lines uniqe and print for every line how many time is it repeated. – Student Jun 15 '19 at 13:25
  • This code is working but, I want to add a command tho print lines uniqely and how many times is that line repeated. – Student Jun 15 '19 at 13:30
  • 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
  • I think, as the question is stated, `echo -n` will do it. If this does not do it for you, then you need to edit the question. – ctrl-alt-delor Jun 15 '19 at 15:28
  • 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

2 Answers2

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;
ingopingo
  • 807
  • 5
  • 7
  • 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
-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.
ebvjr
  • 174
  • 7
  • 1
    [Security implications of forgetting to quote a variable in bash/POSIX shells](https://unix.stackexchange.com/q/171346/237982) – jesse_b Jun 15 '19 at 13:46
  • 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
  • I didn't test it tbh hahaha my bad. – ebvjr Jun 15 '19 at 14:58
  • 1
    `(╯°□°)╯︵ ┻━┻`. – jesse_b Jun 15 '19 at 15:10