#!/bin/sh -u # $0 pathname # Read a text string and look for it in the argument pathname. # Find lines that don't contain the string and put them in a file. # -IAN! idallen@ncf.ca export PATH=/bin:/usr/bin umask 022 # Must have one argument (a file name). # if [ $# -ne 1 ]; then echo 1>&2 "$0: Expecting 1 argument; found $# ($*)" exit 1 fi # Make sure it is a file and is readable. # if [ ! -f "$1" ]; then echo 1>&2 "$0: '$1' is not a file" exit 1 fi if [ ! -r "$1" ]; then echo 1>&2 "$0: '$1' is not a readable file" exit 1 fi # Make sure the last line has the expected text. # last=$(tail -1 "$1") if [ "$last" != "END OF FILE" ]; then echo 1>&2 "$0: Last line of '$1' is not correct." echo 1>&2 "$0: Last line is: $last" exit 1 fi # Count and display the lines in the file. # count=$(wc -l <"$1") echo "File '$1' contains $count lines." # Prompt for (on stderr), read, and echo (on stdout) a text string. # echo 1>&2 "Please enter a text string:" read str echo "You entered '$str'." # Count the lines containing the text string. # Could also use "grep -c". # grepcount=$(grep "$str" "$1" | wc -l) echo "String '$str' appears on $grepcount lines." # If the text string doesn't appear on every line, save # the lines that don't contain it in another file. # Change the permissions on the file, and then rename it. # if [ "$grepcount" -lt "$count" ]; then f="no_string_found$$.txt" grep -v "$str" "$1" >"$f" chmod go-rwx "$f" ls -l "$f" mv "$f" unfound.txt fi