How can I output a list of commands sorted by user?

duckyPluck

I'm trying to have a list of processes that were run sorted by the user who ran them. i have an input file that looks like this:

1 UID PID PPID C STIME TTY TIME CMD
2 adz110 5344 5334 0 08:47 pts /2 00:00:00 bash
3 dmq292 6908 6854 0 Jun04 pts /1 00:00:00 bash
4 adz110 7227 7150 0 Jul11 pts /9 00:00:00 who
5 erg474 7466 7461 0 08:54 pts /10 00:00:00 ls
6 dmq292 7966 7960 0 Jun04 pts /13 00:00:00 assign1 . sh if of
7 xle135 8983 8636 0 08:59 pts /15 00:00:00 ssh ctf . cs . utsarr . net
8 zeh458 9057 1980 0 08:59 pts /7 00:00:00 vim prog . c
9 rslavin 9150 9139 0 08:59 pts /16 00:00:00 ps - af
10 xle135 8636 8628 0 08:58 pts /15 00:00:00 bash

Unfortunately I'm unable to use any options of the sort command for this assignment. I can use sort, just no options. Primarily, I'll have to use awk for this. I have the output pretty close to how I need it, just not quite there. This is how it prints out:

User: adz110
       bash
User: dmq292
       bash

User: erg474
       ls

User: xle135
       ssh
User: zeh458
       vim
User: rslavin
       ps

So, it no longer lists adz110, dmq292, and xle135 twice. However, the second command that each user ran is not there. Tinkering around with it, I did get it to print out that information, but it was the whole line and it would print out one user below where I needed it.

gawk '/^[a-z]/ { if(!x[$1]++) print "User: "($1)"\n\t""   "$8; else if(x[$1]++) print "\t   "x[$NF] };

'

This was supposed to be the end result:

User : adz110
2           bash
3           who
4 User : dmq292
5           bash
6 assign1 . sh if of
7 User : erg474
8           ls
9 User : xle135
10          bash
11          ssh ctf . cs . utsarr . net
12 User : zeh458
13          vim prog . c
RavinderSingh13

Could you please try following(using sort + awk combination).

EDIT1: As per Ed sir's nice advice, I am adding following command which will make sure in case timings are coming inside command it should NOT remove them.

Let's consider following is the Input_file:

cat Input_file
1 UID PID PPID C STIME TTY TIME CMD
2 adz110 5344 5334 0 08:47 pts /2 00:00:00 bash
3 dmq292 6908 6854 0 Jun04 pts /1 00:00:00 bash
4 adz110 7227 7150 0 Jul11 pts /9 00:00:00 who
5 erg474 7466 7461 0 08:54 pts /10 00:00:00 ls
6 dmq292 7966 7960 0 Jun04 pts /13 00:00:00 assign1 . sh if of
7 xle135 8983 8636 0 08:59 pts /15 00:00:00 ssh ctf . cs . utsarr . net
8 zeh458 9057 1980 0 08:59 pts /7 00:00:00 vim prog . c
9 rslavin 9150 9139 0 08:59 pts /16 00:00:00 ps - af
10 xle135 8636 8628 0 08:58 pts /15 00:00:00 bash
4 adz110 7227 7150 0 Jul11 pts /9 00:00:00 grep 'pts /7 '11:00 foo'
4 adz110 7227 7150 0 Jul11 pts /9 00:00:00 grep 1 UID PID PPID C STIME TTY TIME CMD

Now code is as follows:

sort -k2 Input_file | 
awk '
$0 ~ /^1 UID PID PPID C STIME TTY TIME CMD$/{
  next
}
prev!=$2 && prev{
  print "user:   " prev ORS "\t" val
  prev=val=""
}
{
  prev=$2
  match($0,/[0-9][0-9]:[0-9][0-9]:[0-9][0-9].*/)
  match_val=substr($0,RSTART+RLENGTH)
  val=(val?val ORS "\t":"")match_val
}
END{
  if(prev){
    print "user:   " prev ORS "\t" val
  }
}'

Output will be as follows:

user:  adz110
        bash
        grep 1 UID PID PPID C STIME TTY TIME CMD
        grep 'pts /7 '11:00 foo'
        who
user:  dmq292
        bash
        assign1 . sh if of
user:  erg474
        ls
user:  rslavin
        ps - af
user:  xle135
        bash
        ssh ctf . cs . utsarr . net
user:  zeh458
        vim prog . c


sort -k2 Input_file | 
awk '
$0 ~ /UID PID PPID C STIME TTY TIME CMD/{
  next
}
prev!=$2 && prev{
  print "user:   " prev ORS "\t" val
  prev=val=""
}
{
  prev=$2
  sub(/.*:00 /,"")
  val=(val?val ORS "\t":"")$0
}
END{
  if(prev){
    print "user:   " prev ORS "\t" val
  }
}'

Output will be as follows.

user:   adz110
        bash
        who
user:   dmq292
        bash
        assign1 . sh if of
user:   erg474
        ls
user:   rslavin
        ps - af
user:   xle135
        bash
        ssh ctf . cs . utsarr . net
user: zeh458
      vim prog . c

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get this code to output a sorted list instead of "none"

How can i get the input from the user and display the sorted list

how can I count every item in a list by a specific range? Also, why the last item in my output is not sorted?

How is the output of `losetup --list` sorted?

How can I insert a sorted linked list into another sorted one while maintaining it sorted?

How can I create a List with sorted Strings and a specific Integer in Java?

How can I insert elements in an alphabetically sorted list in C?

How can I get a first element from a sorted list?

How can I return a sorted list of related records?

How can I create a sorted list of integer and string pairs?

How can I group the items in a list in sorted order?

How can I get index of sorted list where element changes?

How can I get the list sorted out according to the IP address?

How Can I Find the MIN, MAX, SUM and AVG of A Sorted List

How can I create new lists of a sorted list

How can I return a sorted list of integers here?

How can I run commands and save the output using a .bat file?

How can I quickly scroll down the output of git commands?

How can I send multiple commands' output to a single shell pipeline?

How can I see a history of the terminal output, not just commands entered?

How can I get the raw formatting output of commands?

How can I output a list in random order?

How can I save the output of heapPermutations as a list?

How can I add a sudo user to WSL using commands in powershell?

How can I run these commands against each User profile?

How can I print a list by user input?

How can I make a list with most used commands in terminal?

Can I display a list of sorted items in Grafana?

Regarding this SQL query, how can I output list of 28/30/31 days according to user's choice of certain month and year

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive