Trivial Replace Repeated Lines (fortune quotes reformat)

Here is a small little ditty of bash to mull over for this Thursday’s (2013/08/01) group meet:

(Lots of preamble to set the scene 🙂 )

#!/bin/bash

# Copyright 2013 Martin Lomas
#
# License:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Name:
# formatfortunequotes
#
# Description:
# This takes 'std input', all lines are output unchanged to 'std output' except for:
# Two or more consecutive blank lines are replaced by a single line of '%' suitable for "strfile".
# The output can then be used as input for the utility "strfile" to create a hash indexed .dat file
# for "fortune".
#
# This little ditty has been shown the light of day from long ago as a working example for a
# coding group meeting. This code works, but there are better ways!
#
# Pre-requisites:
# The fortune utility should be available so as to test the results.
# Some quotes text is needed. The quotes are a text file with the individual quotes separated by 2 or more
# blank lines.
#
# Example use:
# ./formatfortunequotes <fortune_quotes.txt >fortune_quotes_formatted
# /usr/sbin/strfile fortune_quotes_formatted
#
# You should now see a file "fortune_quotes_formatted.dat". You can test with:
#
# fortune fortune_quotes_formatted
#
#
# For general use:
# As root, copy or link to the fortune data directory /usr/share/games/fortunes:
# cp fortune_quotes_formatted /usr/share/games/fortunes/fortune_quotes_formatted
# cp fortune_quotes_formatted.dat /usr/share/games/fortunes/fortune_quotes_formatted.dat
#
# OR:
# ln -s $PWD/fortune_quotes_formatted /usr/share/games/fortunes/fortune_quotes_formatted
# ln -s $PWD/fortune_quotes_formatted.dat /usr/share/games/fortunes/fortune_quotes_formatted.dat
#
# Ensure that the path and file permissions allow access.
#
#
# Brief summary:
# strip two or more consecutive zero length lines to a single line containing '%'
#

nlcount=0		# newline count of blank lines

while read line
do
	if [ "x$line" != "x" ]
	then
		[ $nlcount -eq 1 ] && { echo ; nlcount=0 ; }
		[ $nlcount -eq 0 ] && echo "$line"
		[ $nlcount -ge 2 ] && { echo -e "%\n$line" ; nlcount=0 ; }
	else
		(( ++nlcount ))
	fi
done

The above example should spawn a few (better) alternatives. There’s lots of ‘single liners’ for manipulating single lines, but what of conditional multiple lines?…

This is also a good example of how the documentation/comments can far outweigh the code! Can the code itself be ‘obvious’? (Including communicating the programmer’s ‘intent’?…)

See also:

Cheers,
Martin

Other examples for the meeting discussion:

Leave a Reply