-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell Scripting
More file actions
27 lines (17 loc) · 1.29 KB
/
Copy pathShell Scripting
File metadata and controls
27 lines (17 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
ps -eo user,gid,pid,comm --sort=user | awk '{ system("id -gn " $1 " 2>/dev/null") | getline group; $2=group; print }' | grep -vE "[a-z]{2}[0-9]+" | uniq
ps -eo user,gid,pid,comm --sort=user: This lists the user, GID, PID, and the command, sorted by user.
awk: For each user, it uses system("id -gn " $1) to dynamically fetch the group name for the user. It then replaces the GID field with the group name.
getline group: Captures the group name and stores it in the variable group.
2>/dev/null: Suppresses error messages in case id doesn't return anything (for example, if there's an issue with the user lookup).
grep -vE "[a-z]{2}[0-9]+": Filters out unwanted results as per your original command.
uniq: Removes duplicate entries.
*******************************************
ps -eo user,gid,pid,comm --sort=user | while read user gid pid comm; do
group=$(id -gn "$user" 2>/dev/null);
printf "%-10s %-10s %-10s %-10s\n" "$user" "$group" "$pid" "$comm";
done | grep -vE "[a-z]{2}[0-9]+" | uniq
****************************************
ps -eo user,gid,pid,comm --sort=user | (head -n 1 && tail -n +2 | while read user gid pid comm; do
group=$(id -gn "$user" 2>/dev/null);
printf "%-10s %-10s %-10s %-10s\n" "$user" "$group" "$pid" "$comm";
done | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2)