#!/bin/sh # version 1.1 # Usage: rotate < > # - a text file containing the subjects to be rotated # - name for the new file with the rotated subjects # This program assumes that there are no line breaks inside a single 'line' # in the input file awk ' # every line of the input file must be printed to the output file { # this rountine makes sure that no output line is longer than 79 characters if (length ($0) < 80) print $0 else { string = $0 while (length(string) > 79) { for (i = 79; i > 0; i--) if (substr($0,i,1) == " ") break printf "%s\n",substr($0,0,i-1) string = substr($0,i,length($0)-(i-1)) printf "%s", string } printf "\n" } } # if the current record is a subject to be rotated: $1 == "SUBJECT" || $1 == ";" { # count stores how many items are in the subject - must be at least 1 count = 1 for (i = 2; i <= NF; i++) { # '-' delimits each item in the subject if ($i == "-") count++ else # the words in each item are concatonated together so they appear as in the # original, and each item is stored in the array 'subject' subject[count] = subject[count] " " $i } if (count > 1) # if there is more than one item in the subject: for (i = 1; i < count; i++) { # move each item in the array 'back' one space for (j = 1; j <= count - 1; j++) rotated[j] = subject[j+1] # move the first item in the array to the end of the array rotated [count] = subject[1] # concatonate each item into a single string string = ";" for (k = 1; k < count; k++) string = string rotated[k] " -" string = string rotated[k] # print out the string, which is a rotation of the original subject # this rountine makes sure that no output line is longer than 79 characters if (length (string) < 80) print string else { while (length(string) > 79) { for (m = 79; m > 0; m--) if (substr(string,m,1) == " ") break printf "%s\n",substr(string,0,m-1) string = substr(string,m,length(string)-(m-1)) printf "%s", string } printf "\n" } # the new array becomes the array to be rotated for (k = 1; k <= count; k++) subject[k] = rotated[k] } # clear out the array for (i = 1; i <= count; i++) subject[i] = "" } '