I am bit of Bash newbie, so please bear with me here.
I have a text file dumped by another software (that I have no control over) listing each user with number of times accessing certain resource that looks like this:
Jim 109 Bob 94 John 92 Sean 91 Mark 85 Richard 84 Jim 79 Bob 70 John 67 Sean 62 Mark 59 Richard 58 Jim 57 Bob 55 John 49 Sean 48 Mark 46 . . .
My goal here is to get an output like this.
Jim [Total for Jim] Bob [Total for Bob] John [Total for John]
And so on.
Names change each time I run the query in the software, so static search on each name and then piping through wc does not help.
GNU
datamash
:Output:
This sounds like a job for
awk
:) Pipe the output of your program to the followingawk
script:Output:
The
awk
script itself can be explained better in this format:Note, to get the output sorted by score, you can add another pipe to
sort -r -k2
.-r -k2
sorts the by the second column in reverse order:Output:
Pure Bash:
If the first 'done' has no redirection from an input file this script can be used with a pipe:
and sorting the output
The output: