Unix ja shell-ohjelmointi 2001, demo 5 Unix ja shell-ohjelmointi 2001, demo 5 (21.2.2001)

  1. Write a script that

    1. computes the average number of characters and words in a file (with one decimal precision).
    2. computes the relative frequencies of letters in a file as percentages with two decimal places.
    3. counts the number of occurrences of a pair of letters in a file (different letters, in the given order).
    4. finds ten most common words in a file and prints them out along with their counts and percentages with three decimal places.

  2. Command last prints logins in reverse chronological order. How could you count (from the time period last remembers)

    1. how many logins have occurred on each date?
    2. how many logins have occurred in each month?
    3. how many times each user has logged in?
    4. how many different users have logged in on each day of the week on the average?

    1. Write a script that finds the smallest UID above given limit (default 499) that does not yet appear in /etc/passwd.
    2. Write a script that transposes a matrix given as a file (turns rows into columnts), e.g.,

      1 2 3 1 4 7 10
      4 5 6 would become 2 5 8 11
      7 8 9 3 6 9 12
      10 11 12

  3. Write a script that acts as a simple macro processor, interpreting commands like the following:
    a line of the form
    %DEFINE name text
    defines a macro name to have value text, and on every subsequent line %name% will be replaced by text.

    E.g., given input file

    %DEFINE kieli latina
    %DEFINE kissa felis domesticus
    Kissa on %kieli%ksi "%kissa%"!

    it would print
    Kissa on latinaksi "felis domesticus"!

  4. A file has data on machine names and addresses like this:
    euler.it                IN        A         130.234.160.43
    ; vanha nimbus
                            IN        HINFO     HP-9000/835 UNIX
                            INFO      "Tietotekniikka AgC425.1"
                            ALIAS     euler.mit
                            ALIAS     nimbus1.mit
                            ETHER     08000902ED2B
                            IN        MX        0 badmail.it
    
    Lines beginning with semicoln are comments, for indented lines machine name is taken from the previous line (i.e., same as on last non-indented non-comment line). Indentations and field separators can be spaces or tabs. Blank lines are ignored.

    IP addresses are always on 'IN A' line's last field as four dot-separated numbers 0-255.

    Write a script freeip, that finds unused addresses within given range. The range is given as argument like this:
    freeip 192.168.1.1-128
    i.e., only last part as a range.

    By default the initial part is assumed to be 130.234. (i.e., freeip 160.25-128 would be same as freeip 130.234.160.25-128) and if last part is omitted, it'll default to 25-255 (only the third part is required).

  5. A file has data like this:
    KEY ALPHA 3
    KEY BETA 1
    ALPHA note 1 and some junk
    GAMMA junk
    BETA something
    DELTA who cares about this
    KEY DELTA 4
    ALPHA me too
    KEY BETA 0
    DELTA this is important (maybe)
    BETA this is junk
    ...
    
    Write a script that processes it as follows:

    Lines beginnning with KEY define how many fields will be printed for lines beginning with corresponding key.

    E.g., KEY ALPHA 2 means that for lines beginning with ALPHA after that will be printed with two first fields (including the word ALPHA), KEY BETA 0 that lines beginning with BETA will not be printed at all thereafter. So, for the input above it'd print

    ALPHA note 1
    BETA
    ALPHA me too
    DELTA this is important
    
    The number of keys and fields is arbitrary and they can have any visible characters (no spaces, tabs or newlines). Fields are separated by spaces only.


File translated from TEX by TTH, version 1.98.
On 16 Feb 2001, 09:16.