Aligning Texts

is there any way to align my text so every column begins on the same line as the previous line?

here's my command:

printf "THEN ( ${SEARCHPATTB} = Hour[${NTIMEB}] = ${CALTOTB} ) %8s => %8s NOW ( ${SEARCHPATT} = Hour[${NTIMEB}] = ${CALTOT} ) %7s => %7s Reduced By: %7s -${RESULT}"\\n

output i'm currently getting which is not aligned properly:

THEN ( Thu Jan 28 = Hour[02] = 27924 )          =>          NOW ( Thu Feb 04 = Hour[02] = 27018 )         =>         Reduced By:         -3.24452
THEN ( Thu Jan 28 = Hour[03] = 8663 )          =>          NOW ( Thu Feb 04 = Hour[03] = 7396 )         =>         Reduced By:         -14.6254

You don't show where any of your variables are set.

You don't show the arguments being presented to your printf command for the four %s format specifiers in your format string operand.

And, you don't specify the ranges of the numbers you're trying to align in your fields. Making several wild guesses, maybe the following will give you a template you can build on to get what you need:

sep='          =>          '
while gather variable values
do	printf 'THEN ( %s = Hour[%s] = %-8d )%sNOW ( %s = Hour[%s]= %-8d )%sReduced By: %15.6f\n' \
	    "$SEARCHPATTB" "$NTIMEB" "$CALTOTB" "$sep" "$SEARCHPATT" "$NTIMEB" "$CALTOT" "-$RESULT"
done

which should produce aligned output as long as $CALTOT and $CALTOTB expand to values that are in the range -9999999 through 99999999. Note that this produces those two columns left aligned; to right align those columns, change both occurrences of %-8d to %8d .

1 Like

Hi.

From a perl script, align :

#!/usr/bin/env bash

# @(#) s1       Demonstrate text column vertical alignment, align.
# For align source, see:
# http://www.cs.indiana.edu/~kinzler/align/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C align

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
align $FILE

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.3 (jessie) 
bash GNU bash 4.3.30
align 1.7.0

-----
 Input data file data1:
THEN ( Thu Jan 28 = Hour[02] = 27924 )          =>          NOW ( Thu Feb 04 = Hour[02] = 27018 )         =>         Reduced By:         -3.24452
THEN ( Thu Jan 28 = Hour[03] = 8663 )          =>          NOW ( Thu Feb 04 = Hour[03] = 7396 )         =>         Reduced By:         -14.6254

-----
 Results:
THEN ( Thu Jan 28 = Hour[02] = 27924 ) => NOW ( Thu Feb 04 = Hour[02] = 27018 ) => Reduced By: -3.24452
THEN ( Thu Jan 28 = Hour[03] =  8663 ) => NOW ( Thu Feb 04 = Hour[03] =  7396 ) => Reduced By:-14.6254

Automatically detects type of data, aligns accordingly.

See comments in script above for web page for downloading.

Best wishes ... cheers, drl

1 Like