Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

63423 Pgm#2 SED This assignment has two parts p2a and p2b Problem A: sed and gre

ID: 3885811 • Letter: 6

Question

63423 Pgm#2 SED This assignment has two parts p2a and p2b Problem A: sed and grep -f We need a new mechanism to identify accounts that haven't been used in a while so that we can remove the accounts. We will use the lastlog tool to get information of the last time someone logged in; however, it just handles one server. For consistent results (and ease of grading), we have copied the output of lastlog for fox01 and fox02 to /usr/local/courses/clark/cs3423/2017Fa/Proj2/lastlog1.out /usr/local/courses/clark/cs3423/2017Fa /Proj2/lastlog2.out Please note that lastlog returns a result similar to this **Never logged in** ums562 xyz222 fernandez rslavin clark ytang pts/3 173.227.72.99 Sun Aug 7 11:39:56 -0500 2015 pts/11 n0e0d193.cs.uts Wed Jan 13 11:13:49-0600 2016 pts/2 104.218.77.194 Tue Aug 15 10:11:37-05e0 2017 pts/8 172.24.141.44 Fri Aug 18 13:07:29-05002017 **Never logged in** Copy those two lastlog?.out files to your code directory Find anyone who has either not logged in or hasn't logged in during 2017 for each of those lastlog files In the example above, the following users meet the criteria: ums562, xyz222, fernandez, and ytang. The result file should only contain user IDs The intersection of those result files gives us the people who didn't log into both of those servers during the desired time Matching on 2017 Where is the 2017 we want to match which indicates that the user logged in during 2017? If we just match "2017", it could match actual login IDs that contain 2017 who haven't logged in during 2017 Intersection Use grep -f to help with getting the intersection. Unfortunately, some login IDs like "Ip" could match an id like "Ipt913". There are several ways to avoid this problem. Consider having two different sed scripts (one for each of the lastlog?.out files). One of the scripts could produce output which tell the pattern to match to the end of the line ytang$ Ip$ The other sed script produces user IDs without the "$". Now the pattern lp$ doesn't match lpt913 Problem B: sed, cat, sort, and uniq -c For this problem, we will want to get the same result, but with a different approach. Using one of the sed scripts from problem A, we can produce two files of user IDs that either have not logged in or haven't logged in during 2017 (i.e., we won't have a pattern like "Ip$" in our files) Suppose we cat those files together and sort them . We can use uniq -c to get a count of each unique value

Explanation / Answer

In the question logs for 2 servers, fox01 and fox 02 given.

/usr/local/courses/clark/cs3423/2017Fa/Proj2/lastlog1.out

/usr/local/courses/clark/cs3423/2017Fa/Proj2/lastlog2.out

Sample log file looks like

ums562                                                                                                **Never Logged in**

xyz222                  pts/3                     173.227.72.99                     Sun    Aug 7   11:39:56 -0500 2015

fernandez           pts/11                   n0000d193..cs.uts            Wed Jan 13 11:13:49 -0600   2016

rslavin                   pts/2                     104.218.77.194                 Tue    Aug 15 10:11:37 -0500 2017

clark                       pts/8                     172.24.141.44                     Fri     Aug   18 13:07:29 -0500 2017

ytang                                                                                     **Never Logged in**

xyz2017                pts/12                   175.220.54.98                     Mon Aug 20 08:15:30   -0600 2014

-> Copy those last log file to your home directory.

(syntax for coping multiple file to a directory : cp file1 file2 .... <directory>)

cp /usr/local/courses/clark/cs3423/2017Fa/Proj2/lastlog1.out /usr/local/courses/clark/cs3423/2017Fa/Proj2/lastlog2.out /home/user123/code

you need to provide your code directory in the place of /home/user123/code. To know about your current working directory you can use pwd command.

-> p2aDollar.sed : Procuces a user id file with $ after the user ids

create a file named p2aDollar.sed and write below command line in it.

grep -v '2017$' lastlog1.out | awk '{print $1}' | sed 's/.*/&$/' > used_id1

used_id file will contain used ids like below

> cat user_id

ums562$

xyz222$

fernandez$

ytang$

xyz2017$

Explanation :

grep '2017$' sample.txt will return the lines which are ending with 2017, if we will add -v option the result get reversed.

grep -v '2017$' sample.txt , provides the lines which doesn't contain 2017 at the last.

Then the output of grep is passed to awk '{print $1}' through pipe line which returns only the first column(user id).

Then the output is passed to sed 's/.*/&$/ . " /.*/" searches for any line containing any character and then replace with $ added to the search result. & represent the last search result which is the user id itself and replaces with userid$. $ is used for avoiding sed command to treat $ as a special character.

->p2a.sed : Procuces a user id file without $ after the user ids.

create a file named p2aDollar.sed and write below command line in it.

grep -v '2017$' lastlog2.out | awk '{print $1}' > user_id2

the above command will get the user id and store it into user_id2 file without any $ at the end.

->p2a.bash

(create a bash file p2a.bash)

#!/bin/bash

sh p2aDollar.sed

sh p2a.sed

grep -f user_id1 user_id2 > p2a.out

In the above script user_id1 is created with usernames and combined with $. Then the user_id2 which contain only the user name list.

Finally in the grep command we are passing the search pattern in a file which is username$ and searching in user_id2 file will provide the exact match. The result then stored into p2a.out.

cat p2a.out

ums562

xyz222

fernandez

ytang

xyz2017