Can use help with shell scripting and the awk command in Linux/UNIX please: The
ID: 3792799 • Letter: C
Question
Can use help with shell scripting and the awk command in Linux/UNIX please:
The /etc/group file defines the groups to which users belong.
Its format is as followed:
Group : password : Group ID : Group List
Group List of usernames for users who are members of the group Password is an encrypted field and will usually show “x”
Group is the name of the group, shown in the group field of ls –l
Please write an awk script, called activity5.3-5.awk, that displays the group name and usernames of group members. Groups without members shall not be displayed. Below is a report format example.
Also...please provide the command to run the script
List of Groups and users Group Name UserID ----------------- --------- root root
daemon root, bin, daemon ......
This conclude the listing
Explanation / Answer
In this program i am also using the /etc/passwd file which holds the information about all the users. Each row has entry per user.The entries in /etc/passwd file is also separated by ":" and the fourth field gives the group id of the user.
Execution of script :
Step 1) first make the file executable
Command : chmod +x activity5.3-5.awk
Step 2) execute the script
Command : ./activity5.3-5.awk /etc/group /etc/passwd
PROGRAM :
#! /bin/awk -f
BEGIN{
i = 1;
j=1;
k=1;
group_name[500];
group_id[500];
output[500];
}
{
#Reading 1st file
if(NR == FNR){
split($1,val,":");
group_name[i] = val[1];
group_id[i] = val[3];
i = i + 1;
}
else{
# Reading second file
split($1,str,":");
#extract group id of the user
user_group = str[4];
username = str[1];
for(j=1; j < i;j = j + 1){
if( user_group == group_id[j]){
if(length(output[j]) > 0)
output[j] = output[j] "," username;
else
output[j] = username;
}
}
}
}
END{
for (k=1;k < i ;k++){
if(length(output[k]) > 0){
print group_name[k] " " output[k];
}
}
}
Output i got on my linux box :
root root,sync,shutdown,halt,operator
bin bin,vboxadd
daemon daemon
adm adm
lp lp
mail mail
ftp ftp
nobody nobody
users games
dbus dbus
polkitd polkitd
......
......
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.