Unix commands -- Provide the command, script file content and the output for the
ID: 3828666 • Letter: U
Question
Unix commands --
Provide the command, script file content and the output for the below questions
18. Write a script file to print the Last Name, First Name and email address of a student with a header and a footer.
19. Refer to the file groups and print the list of user names and their groups. Use a script file with a header and footer
20. Refer to the file groups and write an awk script file which would accept a userID input and display the group name for that userID as output.
GROUPS FILE
groups (Column 1- Group ID, Column 2: Group Name, Column 3: UserID)
1,Griffindor,hpotter
2,Hufflepuff,cdiggory
4,Slytherin,ssnape
3,Ravenclaw,cchang
1,Griffindor,rweasley
1,Griffindor,hgranger
thanks in advance!!
Explanation / Answer
18. Write a script file to print the Last Name, First Name and email address of a student with a header and a footer.
Ans: For this question i do not find data in groups file indicating lastname, firstname. So i have created a similar file
called groups_1 to provide the command. Below are details.
Input file:
186590cb0725:Chegg bonkv$ cat groups_1
groupId,lastname,firstname,emailId
1,XYZ,Griffindor,hpotter@edu.com
2,ABC,Hufflepuff,cdiggory@edu.com
4,DEF,Slytherin,ssnape@123.com
3,GHI,Ravenclaw,cchang@456.com
1,JKL,Griffindor,rweasley@abc.com
1,MNO,Griffindor,hgranger@def.com
Command:
186590cb0725:Chegg bonkv$ awk -F"," 'BEGIN{print "----------------------------";print "LastName FirstName EmailAddress";print "----------------------------";}NR>1{print $2,$3,$4}END{print "----------------------------";}' groups_1
output:
----------------------------
LastName FirstName EmailAddress
----------------------------
XYZ Griffindor hpotter@edu.com
ABC Hufflepuff cdiggory@edu.com
DEF Slytherin ssnape@123.com
GHI Ravenclaw cchang@456.com
JKL Griffindor rweasley@abc.com
MNO Griffindor hgranger@def.com
----------------------------
19. Refer to the file groups and print the list of user names and their groups. Use a script file with a header and footer
Ans:
Input file:
186590cb0725:Chegg bonkv$ cat groups
1,Griffindor,hpotter
2,Hufflepuff,cdiggory
4,Slytherin,ssnape
3,Ravenclaw,cchang
1,Griffindor,rweasley
1,Griffindor,hgranger
Execution and its output:
186590cb0725:Chegg bonkv$ awk -F"," 'BEGIN{print "---------------------";print "USERID GROUPNAME";print "---------------------";}{print $3,$2}END{print "----------------------"}' groups
output:
---------------------
USERID GROUPNAME
---------------------
hpotter Griffindor
cdiggory Hufflepuff
ssnape Slytherin
cchang Ravenclaw
rweasley Griffindor
hgranger Griffindor
----------------------
20. Refer to the file groups and write an awk script file which would accept a userID input and display the group name for that userID as output.
Ans: There are two ways you can write the scipt. Following are the details.
1) Here you can pass userid like "hpotter" in input and it will print the group name as output for that userID. This is one-liner
and simple.
186590cb0725:Chegg bonkv$ awk -F"," '/hpotter/{print $2}' groups
Griffindor
186590cb0725:Chegg bonkv$ awk -F"," '/cchang/{print $2}' groups
Ravenclaw
2) Doing the above by writing script in a file
code:
#!/bin/bash
# script name: printGroup.sh
# print group name for a given userId from groups file
echo "Enter userId: "
read userid
awk -v uid="$userid" '
BEGIN{
FS=",";
}
$0~uid{
print "Group name is : " $2;
}' groups
Execution and its output:
186590cb0725:Chegg bonkv$ ./printGroup.sh
Enter userId:
hpotter
Group name is : Griffindor
186590cb0725:Chegg bonkv$ ./printGroup.sh
Enter userId:
cchang
Group name is : Ravenclaw
186590cb0725:Chegg bonkv$ ./printGroup.sh
Enter userId:
hgranger
Group name is : Griffindor
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.