For problem A only use sed, grep -f and bash (to launch them) Problem A: sed and
ID: 3888465 • Letter: F
Question
For problem A only use sed, grep -f and bash (to launch them)
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.
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 "lp" could match an id like "lpt913". 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$
lp$
The other sed script produces user IDs without the "$". Now the pattern lp$ doesn't match lpt913.
for problem B only use sed, cat, sort, uniq -c, and bash (to launch them)
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 "lp$" 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
· We can then use sed to include only user IDs with a count of 2 and also remove the count.
Although not required, you probably should diff the outputs of the two solutions. Note that you may have to sort the output of problem A before doing the diff.
lastlog1.out
Username Port From Latest
root pts/6 crush.cs.utsarr. Tue Aug 15 10:52:33 -0500 2017
daemon **Never logged in**
bin **Never logged in**
sys **Never logged in**
sync **Never logged in**
games **Never logged in**
man **Never logged in**
lp **Never logged in**
mail **Never logged in**
news **Never logged in**
uucp **Never logged in**
proxy **Never logged in**
www-data **Never logged in**
backup **Never logged in**
list **Never logged in**
irc **Never logged in**
gnats **Never logged in**
nobody **Never logged in**
libuuid **Never logged in**
syslog **Never logged in**
messagebus **Never logged in**
landscape **Never logged in**
sshd **Never logged in**
ubuntu pts/0 mangw.cs.utsarr. Wed Dec 16 16:49:41 -0600 2015
statd **Never logged in**
usbmux **Never logged in**
lastlog2.out
Username Port From Latest
root pts/0 crush.cs.utsarr. Tue Aug 15 09:47:07 -0500 2017
daemon **Never logged in**
bin **Never logged in**
sys **Never logged in**
sync **Never logged in**
games **Never logged in**
man **Never logged in**
lp **Never logged in**
mail **Never logged in**
news **Never logged in**
uucp **Never logged in**
proxy **Never logged in**
www-data **Never logged in**
backup **Never logged in**
list **Never logged in**
irc **Never logged in**
gnats **Never logged in**
nobody **Never logged in**
libuuid **Never logged in**
syslog **Never logged in**
messagebus **Never logged in**
landscape **Never logged in**
sshd **Never logged in**
ubuntu pts/0 mangw.cs.utsarr. Wed Dec 16 16:49:41 -0600 2015
statd **Never logged in**
usbmux **Never logged in**
Explanation / Answer
Problem A :
-> Two log files are given lastlog1.out and lastlog2.out containing log in information.
-> We need to find out the users who has either not logged in or hasn't logged in during 2017 for each of those lastlog files.
lastlog1.sed : Procuces a user id file wit $ after the user ids.
grep -v '2017$' lastlog1.out | sed 's/ .*//' | sed 's/.*/&$/' > user_id1
used_id1 file will contain used ids like below
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 sed 's/ .*//' 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.
->lastlog2.sed : Procuces a user id file without $ after the user ids.
create a file named lastlog2.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.
create a bash file problemA.bash as below
->problemA.bash
#!/bin/bash
sh lastlog1.sed
sh lastlog2.sed
grep -f user_id1 user_id2 > problemA.out
#grep -f will take input pattern , that is used_id$ from user_id1 file and search those in user_id2.
#The search result gives the final out put of user ids which is present in both lastlog files and those user have not logged in 2017.
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 problemA.out
ums562
xyz222
fernandez
ytang
xyz2017
problemA.bash can be run to perform the task mentioned in problem statement A.
Problem B :
-> We need to do the same thing as in problem statement A, But in a different approach
we need to modify lastlog1.sed as mentioned below, It will now Procuces a user id file without any $ after the user ids.
grep -v '2017$' lastlog1.out | sed 's/ .*//' > user_id1
Now Both user_id1 and user_id2 contain user_ids like below
ums562
xyz222
fernandez
ytang
xyz2017
Create a bash script as mentioned below
->problemB.bash
#!/bin/bash
sh lastlog1.sed
sh lastlog2.sed
cat user_id1 user_id2 | sort | uniq -c | sed 's/^2/&/' | sed -e 's/^.* //' > problemB.out
Explanation : As per the requirement both the used_id files are sorted first. Then the uniq command is used with the count. The result look like this after the unique -c command
2 backup
2 bin
2 daemon
2 games
2 gnats
2 irc
Then sed 's/^2/&/' filters only the user_id having count more than 2, that means it is present in both the log files. Then sed -e 's/^.* //' is used to eliminate number part and just display user_id as below
backup
bin
daemon
games
gnats
irc
landscape
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.