LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-29-2012, 05:49 AM   #1
frater
Member
 
Registered: Jul 2008
Posts: 120

Rep: Reputation: 23
Aligning output using tabs


I'm using a script to build the following file.

It's a list of email-addresses with an inline comment statement.
I'm now using 2 tabs and a bracket....

As you can see most of them align well, but some addresses are and then it isn't properly aligned.


cat rejecttheselocaladdresses.txt | tr 'a-z' x
Code:
# Axxxxxxxxxxxx xxxxxxxxxx xx /xxx/xxxx.xxxxxx/xxxx_xxxxxxxxxxxxxxxxxxxxxxxxx (/xxx/xxxxx/xxxx/xxxx_xxxxxxxxxxxxxxxxxxxxxxxxx)

################################################
# Rxxxxx xxxxx xxxxxxxxx
################################################
xxxx7@xxxxxxxxxxxx.xx           # 1351261071
xxxxxx@xxxxxxxxxxxx.xx          # 1351261071
xxx@xxxxxxxxxxx.xx              # 1351262229
xxxxxxxxxxxxx@xxxxxxxxx.xx              # 1351260071
xxxxxxxxxxxxxxxx@xxxxxxxxx.xx           # 1351261071
xxxxxx@xxxxxxxxx.xxx            # 1351261071
xxxxxx@xxxxxxxxx.xxx            # 1351263110
xxxxxx@xxxxxxxxx.xxx            # 1351268221
xxxx@xx-xxxx.xx         # 1351262229
xxxxxxxx@xx-xxxx.xx             # 1351491421
I could use printf to pad spaces and have it properly aligned, but I prefer tabs.
I think there's a very easy way to do this.

It just needs to know the how much space one tab represents.
I can already pad each word so it takes for instance 30 spaces.

Code:
~# echo 'xxxxxxxxxxxxxx@xxxxxxxxx.xx' | awk '{ printf "%-35s\t# ", $1}' ; date +%s
xxxxxxxxxxxxxx@xxxxxxxxx.xx             # 1351504268
~# echo 'xxxx@xxxxxxxxx.xx' | awk '{ printf "%-35s\t# ", $1}' ; date +%s
xxxx@xxxxxxxxx.xx                       # 1351504281
I'm sure it can be done more elegant

Currently I'm building the file like this (TMP4 has a list of email-addresses):

Code:
grep -v -f ${TMP2} ${TMP3} | sed "s/.*/&\t\t# ${TIMESTAMP}/g" >${TMP4}

Last edited by frater; 10-29-2012 at 05:57 AM.
 
Old 10-29-2012, 06:10 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394
I realize that you like tab's, but I would steer clear from them whenever possible.

The amount of spaces a tab consists of isn't a fixed thing and can be changes per program/shell/OS/user. What looks perfectly aligned on your box might look different on another box or, if you put the output in a file, the command used to print that file might use a different tab length.

Using printf will fix this problem. You can create fixed length columns that neatly aligns your output.
 
2 members found this post helpful.
Old 10-30-2012, 01:01 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,845

Rep: Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006
As druuna said. In computer text, a 'tab' isn't a fixed size character, and the output depends on the settings of the displaying program. The positioning depends on a complex calculation that takes into account the number of characters before the tab, the tab width setting the program uses, and the font used.

In short, there's no way to guarantee the alignment of text when it includes tab characters. Indeed, afaik, you have to use a monospace font to fully ensure alignment, even without them.
 
Old 11-01-2012, 07:13 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.0
Posts: 1,336

Rep: Reputation: 255Reputation: 255Reputation: 255
The OP does not mention, whether it should be output to screen or paper. In case of a Postscript output, it can also be done with proportional fonts left or right aligned:
Code:
#!/bin/sh

X=20
Y=770
TAB=300

exec > demo.ps

echo "%!PS-Adobe-3.0"
echo "/Times-Roman findfont 12 scalefont setfont"

while read ADDRESS NUMBER; do
    echo "$X $Y moveto ($ADDRESS) show ($NUMBER) dup stringwidth pop $TAB exch sub $Y moveto show"
    ((Y-=18))
done < demo.txt

echo "showpage"

exit 0
It will read two columns (yeah, no checking of the input here) and output it left resp. right aligned.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Aligning SSD Partitions LXer Syndicated Linux News 0 03-16-2011 08:32 PM
Aligning sectors for an SSD? DanceMan Linux - Hardware 9 08-06-2010 03:20 AM
Aligning columns by a character shmuck Programming 6 11-17-2006 11:59 AM
Aligning output in C++ R00ts Programming 13 02-14-2006 01:51 PM
aligning torsmo kurrupt Linux - Software 1 06-28-2005 03:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration