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
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.
Comments