Friday, March 31, 2017

Bash: Scripting - trimline

#!/bin/bash
#
function trimline()
{
        MAXLEN=$((LINELEN -3)) # allow space for " \ " at end of line
        if [ "${#1}" -le "${LINELEN}"  ]
        then
                echo "$1"
        else
                echo "${1:0:${MAXLEN}} \\"
                trimline "${1:${MAXLEN}}"
        fi

}
LINELEN=${1:-80} # default to 80 columns (${var:-word} if var is undefined or null then substitute #its value as "word")


while read myline
do
        trimline "$myline"
done

        echo "LINELEN: $LINELEN"
        echo "MAXLEN: $MAXLEN"
~


# cat /home/kensipe/sandbox/scripts/data.d/data1.txt | ./trimline.sh 50

No comments:

Post a Comment