In: Computer Science
IN LINUX/UNIX
1. Based on the example “awkc7” introduced in the handouts, please write an awk script to display the first six records in “loginfile.” Please test your script to make sure the script displays the following information:
ics325sp200221 pts/6 75.168.197.229 Wed Apr 29 22:09 - 23:27 (01:17)
ics325sp200221 pts/10 75.168.197.229 Wed Apr 29 22:04 - 22:07 (00:02)
ics325sp200220 pts/10 24.118.187.116 Wed Apr 29 15:28 - 15:30 (00:02)
ics325sp200220 pts/10 24.118.187.116 Wed Apr 29 15:22 - 15:23 (00:00)
ics325sp200222 pts/11 68.47.45.2 Wed Apr 29 14:54 - 22:34 (07:39)
ics325sp200220 pts/10 24.118.187.116 Wed Apr 29 14:52 - 15:08 (00:15)
2. How to modify “awkc7” so that we can display the last three records in “loginfile?” (you can assume that we have only 7 records in “loginfile”). Please test your script so that it displays the following information:
ics325sp200222 pts/11 68.47.45.2 Wed Apr 29 14:54 - 22:34 (07:39)
ics325sp200220 pts/10 24.118.187.116 Wed Apr 29 14:52 - 15:08 (00:15)
ics325sp200221 pts/8 75.168.197.229 Wed Apr 29 14:03 - 22:10 (08:07)
3. Based on the example “awkc7” introduced in the handouts, please write an awk script to display the records from the third to fifth in “loginfile.” Please test your script to make sure the script displays the following information:
ics325sp200220 pts/10 24.118.187.116 Wed Apr 29 15:28 - 15:30 (00:02)
ics325sp200220 pts/10 24.118.187.116 Wed Apr 29 15:22 - 15:23 (00:00)
ics325sp200222 pts/11 68.47.45.2 Wed Apr 29 14:54 - 22:34 (07:39)
4. As we know, the command “df –h” will return the following message:
Filesystem Size Used Avail Use% Mounted on udev 3.9G 4.0K 3.9G 1% /dev tmpfs 799M 800K 798M 1% /run /dev/dm-0 97G 44G 48G 48% / none 4.0K 0 4.0K 0% /sys/fs/cgroup none 5.0M 0 5.0M 0% /run/lock none 3.9G 0 3.9G 0% /run/shm none 100M 0 100M 0% /run/user
/dev/sda1 236M 40M 184M 18% /boot
Please write an awk command to parse the output of “df –h” so that the following information is displayed: (hint: study “awkc8” in the handout and “example 7” in lab 7)
Total Used Disk Space: 44G
5. Based on the examples “awkc8” and “awkc88,” write an awk script called “awkc888” that takes two input values and output the larger one as shown below:
$ ./awkc888 100 40
100
$
$ ./awkc888 100 240
240
$
# Sample data
# @filename ./loginfile.txt
# ics325sp200221 pts/6 75.168.197.229 Wed Apr 29 22:09 - 23:27 (01:17)
# ics325sp200221 pts/10 75.168.197.229 Wed Apr 29 22:04 - 22:07 (00:02)
# ics325sp200220 pts/10 24.118.187.116 Wed Apr 29 15:28 - 15:30 (00:02)
# ics325sp200220 pts/10 24.118.187.116 Wed Apr 29 15:22 - 15:23 (00:00)
# ics325sp200222 pts/11 68.47.45.2 Wed Apr 29 14:54 - 22:34 (07:39)
# ics325sp200220 pts/10 24.118.187.116 Wed Apr 29 14:52 - 15:08 (00:15)
# ics325sp200221 pts/8 75.168.197.229 Wed Apr 29 14:03 - 22:10 (08:07)
# 1
# Based on the example “awkc7” introduced in the handouts,
# please write an awk script to display the first six records in “loginfile.”
# Please test your script to make sure the script displays the following information:
awk '
NR <= 6 {
print $0
}' ./loginfile.txt
# 2
# How to modify “awkc7” so that we can display the last three records in “loginfile?”
# (you can assume that we have only 7 records in “loginfile”).
# Please test your script so that it displays the following information:
awk '{
for( i=0; i < 2; i++)
a[i] = a[i+1];
a[2] = $0;
}
END {
print a[0],a[1],a[2];
}' OFS='\n' ./loginfile.txt
# 3
# Based on the example “awkc7” introduced in the handouts,
# please write an awk script to display the records from the third to fifth in “loginfile.”
# Please test your script to make sure the script displays the following information:
awk '
NR >= 3 && NR <= 5 {
print $0
}' ./loginfile.txt
# 4
# As we know, the command “df –h” will return the following message:
# Please write an awk command to parse the output of “df –h” so that the following information is displayed:
# (hint: study “awkc8” in the handout and “example 7” in lab 7)
df -h ./loginfile.txt |
awk '1 < NR {
print "Total Used Disk Space: " $3;
}'
# 5
# Based on the examples “awkc8” and “awkc88,” write an awk script called “awkc888”
# that takes two input values and output the larger one as shown below:
awk 'BEGIN {
if (ARGV[1] < ARGV[2]) {
print ARGV[2]
} else {
print ARGV[1]
}
}' $1 $2