INF3110/4110 Problems week 37 (6.-10.9.2004) Problem 1 Write a Perl program which reads one or more text files and prints the number of non-blank lines. For example, assume that the file `short.file? contains the following four lines: First line ^T Fourth line (Line two contains just a TAB character, while the third line is empty.) The program `nonblank.perl? will then work like this: % nonblank.perl short.file 2 Problem 2 The match pattern used in the previous problem is ^\s*$ What will match if the pattern is a) \s*$ b) ^\s+ c) \s* d) \s+ e) \s Problem 3 The standard Unix program `tail? prints the last n lines of a text file (by default n=10). Write this program in Perl. The program should accept one file name and maybe an option that gives the number of lines to print; if this option is not present, the last 10 will be printed. Hint. The simplest way of finding the last n lines is to read all the lines into an array. Example: % tail.perl short.file First line ^T Fourth line % tail.perl -3 short.file ^T Fourth line % tail.perl -1 short.file Fourth line % Problem 4 Write a program `extract.perl? which will print the specified lines from a file. It will accept two parameters: a line number range and a file name. The line numbers may be given as 4:17 lines 4, 5, ..., 17 6 line 6 2: line 2, 3, ... :5 line 1, 2, 3, 4, 5 Example % extract.perl 4 short.file Fourth line % extract.perl 3: short.file Fourth line % Problem 5 The standard Unix command `wc? (for "word count") counts the number of lines, words and characters in a file. Write this program in Perl. Example % wc short.file 4 4 26 short.file Hint. To count the number of words in a line, try to substitute the word with nothing using the construct s/...// (where you must find a suitable pattern ...) as many times as you can. When the substitution returns 0, you know that there are no more words. Hint. The pattern `\s? matches any space character, while `\S? matches any non-space character. Problem 6 The real Unix `wc? can handle several files. It will produce counts for each file and the a sum of all these. Modify your program from the previous problem to handle this. Example Assume that file `empty.text? is empty. % wc empty.text 0 0 0 empty.text % wc short.file empty.text short.file 4 4 26 short.file 0 0 0 empty.text 4 4 26 short.file 8 8 52 total Problem 7 The Unix program `last? tells of all users who have logged on to the computer for some time: chyunc pts/3 tournai.uio.no Wed Sep 1 17:54 - 17:58 (00:04) cristobd pts/17 Wed Sep 1 17:10 - 17:25 (00:15) cristobd pts/12 benabarre.ifi.ui Wed Sep 1 17:10 - 17:25 (00:15) christkk pts/3 Wed Sep 1 17:09 - 17:14 (00:04) christkk pts/9 Wed Sep 1 17:09 - 17:14 (00:04) christkk X4744 tauste.ifi.uio.n Wed Sep 1 17:09 - 17:14 (00:04) cjhaga pts/31 montecarlo.uio.n Wed Sep 1 15:43 - 18:04 (02:20) christkk pts/37 Wed Sep 1 15:16 - 15:17 (00:00) christkk pts/27 Wed Sep 1 15:11 - 15:37 (00:26) christkk pts/28 Wed Sep 1 15:11 - 15:37 (00:26) christkk X4744 tauste.ifi.uio.n Wed Sep 1 15:11 - 15:37 (00:26) chrisvid pts/57 Wed Sep 1 12:46 - 13:24 (00:38) chrisvid X46ba ainodake.ifi.uio Wed Sep 1 12:45 - 13:24 (00:38) chrisjo pts/19 madero.ifi.uio.n Wed Sep 1 10:54 - 14:03 (03:09) chrisoh pts/9 169.80-202-71.ne Wed Sep 1 08:15 - 15:50 (1+07:34) The first column gives the user's account name and the last one how long the user was logged on. The notation "1+07:34" means "1 day (i.e., 24 hours) plus 7 hours and 34 minutes. Write a Perl program that reads the log produces by `last? and print an alphabetically sorted list of who has logged in on the computer, how many times, and their average connection time. Example If the log lines given above constitute the complete log, the result should be chrisjo once; average 189 min chrisoh once; average 454 min christkk 7 times; average 13 min chrisvid 2 times; average 38 min chyunc once; average 4 min cjhaga once; average 140 min cristobd 2 times; average 15 min