Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-04-2012
Registered User
 
Join Date: Jun 2012
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
Question 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
[code]
>$TEST
WELCOME
HI
HELLO
UNIX
SHELL
SCRIPTING
>$
I want to display the content like

Code:
.
TEST                      WELCOME        HI
HELLO                   UNIX                 SHELL
SCRIPTING   ....
.

Here we need to consider the longest word of the file( in this ex SCRIPTING = 9),So after each word there should me some empty spaces,
ex: TEST has only 4 letters in it, So I need 5 empty space following TEST so 4+5 = 9...like this i need to display the content

( Here in this site the empty spaces are not coming)

Please help me to fix this..I'm trying this for since 10 days..couldn't figure it out...Thanks in advance...

Moderator's Comments:
Please use next time code tags for your code and data

Last edited by rajmohan146; 06-04-2012 at 10:23 AM..
Sponsored Links
    #2  
Old 06-04-2012
vbe's Avatar
vbe vbe is online now Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,633
Thanks: 118
Thanked 256 Times in 245 Posts
If you had used code tags, you would have noticed it keeps spacing and indent format...
Sponsored Links
    #3  
Old 06-04-2012
vbe's Avatar
vbe vbe is online now Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,633
Thanks: 118
Thanked 256 Times in 245 Posts
Ever considered using pr command ? (Its magic...)
    #4  
Old 06-04-2012
Registered User
 
Join Date: Jun 2012
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks
Sponsored Links
    #5  
Old 06-04-2012
sdf sdf is offline
Registered User
 
Join Date: Jun 2011
Location: +49° 59' 55.69", +8° 16' 27.69"
Posts: 113
Thanks: 40
Thanked 5 Times in 5 Posts

Code:
awk 'BEGIN{ORS=""} {printf("%-9s", $1)}' infile >outfile

Sponsored Links
    #6  
Old 06-04-2012
Registered User
 
Join Date: Jun 2012
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
But I need to print more than one word( say TEST , HI, HELLO) in a same line...
Like matrix format

---------- Post updated at 09:36 AM ---------- Previous update was at 09:24 AM ----------

Sdf

Thanks for the help

I need the counter to be reseted when starting of every line..I mean the output line

Last edited by rajmohan146; 06-06-2012 at 02:52 AM..
Sponsored Links
    #7  
Old 06-04-2012
sdf sdf is offline
Registered User
 
Join Date: Jun 2011
Location: +49° 59' 55.69", +8° 16' 27.69"
Posts: 113
Thanks: 40
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by rajmohan146 View Post

I need the counter to be reseted when starting of every line..
Are there several files and for every file the longest word shall be the measure for the field length?

---------- Post updated at 10:25 PM ---------- Previous update was at 04:42 PM ----------

@ rajmohan146

This script determines the longest word of the file and paddes the rest of the field with empty spaces according to the longest word of the file:
Code:
awk '{a[i++]=$1; if(length($1) > length(max)) max=$1;l=length(max)}  END{ for(j=1; j<=i;j++) printf("%"(l+1)"s,",a[j-1])}' infile > outfile

output

Code:
      TEST,   WELCOME,        HI,     HELLO,      UNIX,     SHELL, SCRIPTING,

Good luck!

Last edited by sdf; 06-05-2012 at 03:02 AM.. Reason: adjusted script
The Following User Says Thank You to sdf For This Useful Post:
rajmohan146 (06-06-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Aligning columns in a text file using Perl smarty86 Shell Programming and Scripting 8 04-20-2012 06:01 AM
Aligning numbers in the second file based on first file shoaibjameel123 Shell Programming and Scripting 4 09-08-2011 12:57 AM
Aligning text files by max field length physalis2099 Shell Programming and Scripting 7 08-11-2011 06:30 PM
Aligning for boundary conditions axes Programming 3 07-26-2010 01:01 PM
Aligning Characters in Bash/Beautiful Screens de_la_espada UNIX for Dummies Questions & Answers 1 08-02-2009 07:46 AM



All times are GMT -4. The time now is 10:17 AM.