Aligning Texts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Aligning Texts
# 1  
Old 02-07-2016
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:

Code:
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:

Code:
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

# 2  
Old 02-07-2016
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-07-2016
Hi.

From a perl script, align:
Code:
#!/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:
Code:
$ ./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
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Aligning a file

I have a large text file in following format cat input.txt abc qwert qwer afweferf wdfwefwe ==> kjhjkwdd mnmn ==> jkjkjwekj poiu ==> lklklke tytyutut ==> olkjmnsmn I need to align those lines with the characters " ==>" . I dont want to disturb the lines which dont have "==>". The... (6 Replies)
Discussion started by: ctrld
6 Replies

2. Shell Programming and Scripting

Aligning data

Hi Guys, How can you align data getting the UID, GID HOSTNAME in same column input: server1 uid=1010(faculty) gid=700(teacher) groups=700(teacher), 800(models) student:x:500:500:student:/home/student:/bin/bash server2 uid=1010(OSAD) gid=700(teacher) groups=700(teacher), 809(staff)... (5 Replies)
Discussion started by: invinzin21
5 Replies

3. UNIX for Dummies Questions & Answers

Texts between 2 strings

Hi, I have a file with texts shown below, <2013 abc <2013 start request pdu dot1q end pdu response pdu dot1q end pdu am searching for the text "dot1q" , when it matches in the file , i need the contents between "<2013 start" and "end pdu". Can some one help on this ? ... (5 Replies)
Discussion started by: giri_luck
5 Replies

4. Shell Programming and Scripting

Aligning output with null fields in shell script

Hello Gurus ! I have what probably amounts to a few simply changes to fix; however for the life of me I cannot seem to get it ti work. I need to align the output of my script (I am writing to a logfile)... here's the lines in my code: if then echo "NODE: $node" >> $logfile... (6 Replies)
Discussion started by: gvolpini
6 Replies

5. UNIX for Dummies Questions & Answers

Help with Aligning the content of a txt file

Hello friends Please help me to display the content of a file in specific aligned manner. for ex. the content of the file may be >$TEST WELCOME HI HELLO UNIX SHELL SCRIPTING >$ I want to display the content like . TEST WELCOME HI HELLO ... (18 Replies)
Discussion started by: rajmohan146
18 Replies

6. Shell Programming and Scripting

Aligning columns in a text file using Perl

Hi All, I am new to perl and was trying to write a simple program which will generate a text file as output.. now the output which i am getting is something like this.. ================================================================================================== Col1 ... (8 Replies)
Discussion started by: smarty86
8 Replies

7. Shell Programming and Scripting

Aligning text files by max field length

Hello, Is there anyway that I can align a pipe delimited text file by the maxium field length where the field is separated out by pipes for large text files with more than 100,000 rows? So, far I have searched other forums and google about aligning text files in unix and I have noticed that... (7 Replies)
Discussion started by: physalis2099
7 Replies

8. Shell Programming and Scripting

extract texts using awk

Hello, I have two files: File1: a b c d File2: b c e I need 'e' as output.... Thanks.. ---------- Post updated at 12:16 PM ---------- Previous update was at 12:15 PM ---------- (1 Reply)
Discussion started by: shekhar2010us
1 Replies

9. Programming

Aligning for boundary conditions

Hi, I have tcp/ip client server programs which will communicate through reqest,reply c-structures. As the sizeof(struct) may give different value between client and server programs, how do i align properly for boundary conditions. Could anybody please give some suggestion. Thanks in... (3 Replies)
Discussion started by: axes
3 Replies

10. UNIX for Dummies Questions & Answers

Aligning Characters in Bash/Beautiful Screens

Howdy, Supposing I want to output the following code to the screen in a bash script, this works fine until you use variables as below, because the variable could be of any length, meaning the screen output for line 2 will have the ultimate # out of alignment. Is there a simple way round this?... (1 Reply)
Discussion started by: de_la_espada
1 Replies
Login or Register to Ask a Question