Lost and could use some help with this problem. Thank you for the help!! 1. Cons
ID: 3736537 • Letter: L
Question
Lost and could use some help with this problem. Thank you for the help!!
1. Consider the file whotb below:
-bash-3.2$ cat whotb
avuong1 pts/0 2017-08-25 23:00 (10.152.100.245)
pbartakk pts/11 2017-08-25 23:29 (174.17.211.48)
zzhan281 pts/3 2017-08-25 22:52 (71.223.35.124)
jgamboa8 pts/2 2017-08-25 23:05 (71.231.248.218)
pkalungi pts/1 2017-08-25 20:12 (10.143.198.41)
ramdisk pts/20 2017-08-22 17:17 (10.106.150.87)
pbartakk pts/8 2017-08-25 22:22 (10.152.112.188)
pkalungi pts/10 2017-08-25 23:18 (10.143.198.41)
a. Display only (in order) the station #, username, and date logged in order
b. Sort the above in increasing order of the station #
c. Sort the same data in reverse order
d. Create a file called useron that contains only the usernames
e. Sort the file useron in reverse order, removing the duplicate, and saving with the same filename
f. Insert line numbers into the file useron above, starting at 100 and in increment of 10
Explanation / Answer
NOTE: I have completed your assignment. Please check and let me know if you find any issues. I will revert back within 24 hours. thanks for your patience.
Original Input file:
Unix Terminal> cat whotb
avuong1 pts/0 2017-08-25 23:00 (10.152.100.245)
pbartakk pts/11 2017-08-25 23:29 (174.17.211.48)
zzhan281 pts/3 2017-08-25 22:52 (71.223.35.124)
jgamboa8 pts/2 2017-08-25 23:05 (71.231.248.218)
pkalungi pts/1 2017-08-25 20:12 (10.143.198.41)
ramdisk pts/20 2017-08-22 17:17 (10.106.150.87)
pbartakk pts/8 2017-08-25 22:22 (10.152.112.188)
pkalungi pts/10 2017-08-25 23:18 (10.143.198.41)
Unix Terminal>
a. Display only (in order) the station #, username, and date logged in order
Ans)
Unix Terminal> cat whotb|awk '{print $2, $1, $3}'
pts/0 avuong1 2017-08-25
pts/11 pbartakk 2017-08-25
pts/3 zzhan281 2017-08-25
pts/2 jgamboa8 2017-08-25
pts/1 pkalungi 2017-08-25
pts/20 ramdisk 2017-08-22
pts/8 pbartakk 2017-08-25
pts/10 pkalungi 2017-08-25
Unix Terminal>
Explanation:
1) cat whotb displays the contents of file whotb
2) awk '{print $2, $1, $3}' displays only station#, username and date
3) cat whotb|awk '{print $2, $1, $3}' displays station#, username and date logged in order
b. Sort the above in increasing order of the station #
Ans)
Unix Terminal> cat whotb|awk '{print $2, $1, $3}'|sort -k1.5 -n
pts/0 avuong1 2017-08-25
pts/1 pkalungi 2017-08-25
pts/2 jgamboa8 2017-08-25
pts/3 zzhan281 2017-08-25
pts/8 pbartakk 2017-08-25
pts/10 pkalungi 2017-08-25
pts/11 pbartakk 2017-08-25
pts/20 ramdisk 2017-08-22
Unix Terminal>
Explanation:
1) cat whotb displays contents of the file whotb
2) awk '{print $2, $1, $3}' displays station #, username and date
3) numerically(-n) sorts on first column starting from the character 5 where the station number starts.
4) cat whotb|awk '{print $2, $1, $3}'|sort -k1.5 -n
sorts the file based on station # increasing order.
c. Sort the same data in reverse order
Ans)
Unix Terminal> cat whotb|awk '{print $2, $1, $3}'|sort -k1.5 -nr
pts/20 ramdisk 2017-08-22
pts/11 pbartakk 2017-08-25
pts/10 pkalungi 2017-08-25
pts/8 pbartakk 2017-08-25
pts/3 zzhan281 2017-08-25
pts/2 jgamboa8 2017-08-25
pts/1 pkalungi 2017-08-25
pts/0 avuong1 2017-08-25
Unix Terminal>
Explanation:
1) cat whotb displays contents of the file whotb
2) awk '{print $2, $1, $3}' displays station #, username and date
3) numerically(-n) sorts on first column starting from the character 5 where the station number starts.
4) cat whotb|awk '{print $2, $1, $3}'|sort -k1.5 -nr
sorts the file based on station # in reverse order
d. Create a file called useron that contains only the usernames
Ans)
Unix Terminal> cat whotb|awk '{print $2, $1, $3}'|sort -k1.5 -nr|awk '{print $2}'>useron
Unix Terminal> cat useron
ramdisk
pbartakk
pkalungi
pbartakk
zzhan281
jgamboa8
pkalungi
avuong1
Explanation:
1) cat whotb displays contents of the file whotb
2) awk '{print $2, $1, $3}' displays station #, username and date
3) numerically(-n) sorts on first column starting from the character 5 where the station number starts.
4) awk '{print $2}'>useron fetches only username and redirects to the output file useron
5) cat whotb|awk '{print $2, $1, $3}'|sort -k1.5 -nr|awk '{print $2}'>useron
creates a file called useron that only contains usernames.
e. Sort the file useron in reverse order, removing the duplicate, and saving with the same filename
Ans)
Unix Terminal> cat useron|sort -r -u > useron_temp
Unix Terminal> mv useron_temp useron
Unix Terminal> cat useron
zzhan281
ramdisk
pkalungi
pbartakk
jgamboa8
avuong1
Unix Terminal>
Explanation:
1) cat useron|sort -r -u > useron_temp
fetches the contents of file useron and sorts it by username in reverse order(-r). Also removes duplicates(-u) then saves the filtered content to useron_temp file
2) mv useron_temp useron
Since we cannot read and write on the same file. We have first fetched the required contents stored the filtered data in useron_temp file then renamed the file using mv command to the original file.
f. Insert line numbers into the file useron above, starting at 100 and in increment of 10
Ans)
Unix Terminal> cat useron
zzhan281
ramdisk
pkalungi
pbartakk
jgamboa8
avuong1
Unix Terminal> seq 100 -10 1 >> useron
Unix Terminal> cat useron
zzhan281
ramdisk
pkalungi
pbartakk
jgamboa8
avuong1
100
90
80
70
60
50
40
30
20
10
Unix Terminal>
Explanation:
1) seq 100 -10 1 will generate numbers from 100 with a step value of 10
2) seq 100 -10 1 >> useron
numbers generated from 100 are appended to the file useron
3) The actual file contents before and after changes are displayed using "cat useron" command.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.