Thursday 21 May 2015

Write a shell script that folds long lines into 40 columns. Thus any line that exceeds 40 characters must be broken after 40th, a “\” is to be appended as the indication of folding and the processing is to be continued with the residue. The input is to be supplied through a text file created by the user.VTU MCA UNIX LAB9B

echo "enter a file name"
read f

n=`wc -l $f | cut -d " " -f 1`
i=1

while [ $i -le $n ]
do
    line=`sed -n "$i p" $f`
    cc=`echo $line | wc -c | cut -d " " -f 1`
    echo "$cc"
    while [ $cc -ge 40 ]
    do
        ext=`echo $line | cut -c 41-`
        line=`echo $line | cut -c 1-40`
        echo "$line \\"
        line=$ext
        cc=`echo $ext | wc -c | cut -d " " -f 1`
    done
echo "$line"   
i=`expr $i + 1`   
done
 

No comments:

Post a Comment