#! /bin/bash # Name: colfmt # Purpose: formats text files delimited by spaces to vertical colums # Syntax: colfmt filename [startline] [spacing] # Version: 1.0 # Author: Michel Bisson(michel@linuxint.com) # Date: 30 Sept 2003 # History: 30 Sept First implementation #----------------------------------------------------------------------------- #-------------- Number of parameters is 1 ? ----------------- if [ $# -lt 1 ] ; then echo "ERROR: Wrong number of parameters" echo "Syntax: colfmt filename [startline] [spacing]" exit 1 fi #-------------- Empty file or not readable ? ---------------- if test ! -r $1 -o ! -s $1 ; then echo "ERROR: File $1 empty or not readable" exit 1 fi #------------- check if a start line has been given, if not then start at line 1 if [ $# -gt 1 ] ; then #--------------- Check if Second parameter is an integer ----------------- if [ $[$2/1] -eq 0 ] ; then echo "ERROR: Second parameter should be an integer" echo "Syntax: colfmt filename [startline] [spacing] " exit 1 else startline=$2 fi else startline=1 fi #--------------- Check if second parameter is bigher than the file number of lines ----- alllines=$(wc -l $1| awk '{print $1}') #echo $alllines ; read dummy if [ $startline -gt $alllines ] ; then echo "ERROR: Second parameter 'startline' is greater than the number of lines in file $1" echo "Syntax: colfmt filename startline" exit 1 fi #-------------- Check if 'spacing' was given as parameter -------- if [ $# -eq 3 ] ; then #--------------- Check if Second parameter is an integer ----------------- if [ $[$3/1] -eq 0 ] ; then echo "ERROR: Third parameter should be an integer" echo "Syntax: colfmt filename [startline] [spacing] " exit 1 else spacing=$3 fi else spacing=1 fi #-------------- Read the file and find out for each collumn how long is the longest word. #-----Read the first line, and start from there in counting the max number of #-----collumns($collnum) as well as the size of the longest word per collumn # ($collmax1, $collmax2, ... #----- collnum=0 line=$startline while [ $line -le $alllines ] ; do linetxt=$(eval "sed -n '$line p' $1") if ! { echo $linetxt | grep "^\#" &>/dev/null ; } ; then coll="0" for c in $linetxt ; do let coll++ word=$(eval "echo $linetxt | awk '{print \$$coll}'") wordwidth=$( echo $word | wc -c ) let wordwidth-- eval "collmax=\${collmax$coll:=0}" if [ $collmax -lt $wordwidth ] ; then eval "collmax$coll=\$wordwidth" fi done test $collnum -lt $coll && collnum=$coll fi let line++ done #--------------- Display the max wordlength per collumn ------------------- #i=1 #while [ $i -le $collnum ] ; do # eval echo -n \$collmax$i"\ -\ " # let i++ #done #echo #-------------- Now go through the file again and format the columns ---------- collnum=0 line=1 while [ $line -le $alllines ] ; do linetxt=$(eval "sed -n '$line p' $1") #---Check for comment lines echo $linetxt | grep "^\#" &>/dev/null if [ ! $? = 0 -a ! $line -lt $startline ] ; then coll="0" for c in $linetxt ; do let coll++ word=$(eval "echo $linetxt | awk '{print \$$coll}'") wordwidth=$(echo $word | wc -c ) let wordwidth-- eval "collmax=\${collmax$coll:=0}" let collmax+=$spacing moresp=$[$collmax-$wordwidth] echo -n $word i=1 while [ $i -le $moresp ] ; do echo -n " " let i++ done done echo else #----------------- Send the comment lines intact without formatting ----------- echo $linetxt fi let line++ done