#!/bin/bash # Copyright 2007 Gilbert Ashley # text wrapper -formats text into lines of a certain number of characters # words which overlap the end of line are moved to the next line -not hyphenated WRAP_LENGTH=72 PKG_NAME=testname OUTPUT_LINE_COUNT=0 OUTPUT="" # collect all the input text into one long line while read LINE ; do STRING="$LINE" # replace newlines with a space STRING=${STRING/'\n'/ /} # add the new text to the OUTPUT with a space between # if there are double spaces they get removed below. OUTPUT="$OUTPUT $STRING" shift done PARSESTRING="$OUTPUT" while [[ $PARSESTRING != "" ]] ; do (( OUTPUT_LINE_COUNT++ )) # read the number of chars matching the wrap length CHUNK=${PARSESTRING:0:${WRAP_LENGTH}} # skip over everything but the last character of the CHUNK OFFSET_LENGTH=$(( $WRAP_LENGTH -1 )) # if the last character is a space or not null shorten the input line to the previous word if [[ "${CHUNK:$OFFSET_LENGTH:1}" = " " ]] || [[ "${CHUNK:$OFFSET_LENGTH:1}" != "" ]] ; then # keep everything before the last space CHUNK=${CHUNK%$" "*} fi # get the length of the finished chunk CHUNK_LENGTH=${#CHUNK} # advance the pointer the length of the chunk that we saved PARSESTRING=${PARSESTRING:$CHUNK_LENGTH} echo $PKG_NAME:"$CHUNK" # exit if we have reached the maximum number of lines if [[ $OUTPUT_LINE_COUNT -gt 12 ]] ; then break else CHUNK="" fi done