Align the words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Align the words
# 1  
Old 11-29-2011
Align the words

HI ,


I am new to shell scripting i m getting the below format like this
Code:
Name                  FirstName            Lastname
------               ---------            ----------
Name1            Balaji                 NandaKishore  
Name123            Vijaya                  krsihna


But I want in the formatted way like as below:


Code:
Name                FirstName            Lastname
------              ---------            ----------
Name1               Balaji                 NandaKishore  
Name123            Vijaya                krsihna


please help me to find out the solution.


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-29-2011 at 08:34 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 11-29-2011
I'm not able to understand the difference between your input and output. Please elaborate.
# 3  
Old 11-29-2011
hi extremely sorry,

Alignement problem

What i found is??

Code:
Name                  FirstName            Lastname
------               ---------            ----------
 Name1            Balaji                 NandaKishore  
Name123            Vijaya                  krsihna



But I want in the formatted way like as below:

Code:
Name FirstName Lastname
------- -------- ---------- Name1 Balaji NandaKishore Name123 Vijaya krsihna

# 4  
Old 11-29-2011
Code:
$ nawk '{ printf "%-8s %-12s %-8s \n" ,$1,$2,$3 }' infile
Name     FirstName    Lastname
------   ---------    ----------
Name1    Balaji       NandaKishore
Name123  Vijaya       krsihna
$

# 5  
Old 11-29-2011
Hi.

The best automatic alignment tool I've found is the perl script align. Here's a demonstration:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate automatic alignment, align.
# http://freecode.com/projects/align

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
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, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
align 1.7.0

-----
 Input data file data1:
Name                  FirstName            Lastname
------               ---------            ----------
Name1            Balaji                 NandaKishore  
Name123            Vijaya                  krsihna


-----
 Results:
Name    FirstName Lastname
------  --------- ----------
Name1   Balaji    NandaKishore
Name123 Vijaya    krsihna

Best wishes ... cheers, drl
# 6  
Old 11-29-2011
You could use expand (it usually works with TAB separated input so I used sed to convert your spaces to tabs first):

Code:
$ sed 's/  */\t/g' infile | expand -t 12
 
Name        FirstName   Lastname
------      ---------   ----------
Name1       Balaji      NandaKishore            
Name123     Vijaya      krsihna

Replace the 12 with any number you like for the column widths or specify tabstops as a comma list eg -t 12,18,35
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Align columns

I have a text file that I filtered using awk. I only exctracted two columns. I want those two columns to be aligned. Most of the answers I found was to use `column -t` command. I tried that but I am getting a `bash: column: command not found`. Is there another way to align columns... (1 Reply)
Discussion started by: erin00
1 Replies

2. Shell Programming and Scripting

Re align in one row using sed

Hi, Anyone can help on how to re align my data in one row using sed. test.csv "url1","abc","project url1" 2016-08-16 "url2,"microsoft","project url2" 2016-08-18 need output like this "url1","abc","project url1","2016-08-16" "url2,"microsoft","project url2","2016-08-18" Thanks in... (8 Replies)
Discussion started by: fspalero
8 Replies

3. Shell Programming and Scripting

Align input fields on one under each other

Hello again, I'm having a issue with a contact form. I want the input fields to be aligned on same row and I really dont know how to do it. I attached a image for you to understand what i am trying to do. <BR> <label for="name" class="required"> Name&nbsp;<strong... (1 Reply)
Discussion started by: galford
1 Replies

4. Shell Programming and Scripting

Align columns

Hi, I have a question on how to align columns in shellscipt. SAMPLE 2015-07-15 09:01:00.0 |TCSERVER01 |10965 2015-07-15 09:02:00.0 |TCSERVER01 |4752 2015-07-15 09:03:00.0 |TCSERVER01 |4805 2015-07-15 09:04:00.0 |TCSERVER01 |3690 2015-07-15 09:01:00.0 |TCSERVER02 |8703 2015-07-15... (1 Reply)
Discussion started by: reignangel2003
1 Replies

5. Shell Programming and Scripting

Align data of the paragraph

Hi, I need your help to align data from paragraph. It's to : - delete the lign contain NDS - align data between paragraph start to "ND=" from second "ND=" example of file: @ ABOIL; @ CEN=1/15-05-23/09 H 06 MN 18/LISTAGE CARACTERISTIQUES D'ABONNES TRAITEMENT TGLAIL ACC... (1 Reply)
Discussion started by: vremen3
1 Replies

6. Shell Programming and Scripting

Align with printf or other method

below line is one by one print audio_play PASS boot_check FAIL ethernet_change_mac NIC_82574L PASS gps_ublox_neo FAIL storage_bonnie USB2 PASS storage_copy_big_file USB2 PASS I would like below format ... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

7. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

8. Shell Programming and Scripting

Align or move paragraph right

Hello I am a newbie in scripting and I am hoping someone may help with a method of aligning or tabbing selected text as its output. For example, using the SED command to extract paragraphs containing AA BBB CCC sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d' How could I say, get the... (18 Replies)
Discussion started by: lostincashe
18 Replies

9. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

10. UNIX for Dummies Questions & Answers

Align Text from a file.

I need to align text from a file that has columns seperated by spaces and commas. Any ideas? Text is similar to this. File Name is Test. 05/14/06 13:46:56.575 ,TEST,5,123,1234,123,12345,12,12.2,2.1,4.5,5.23 05/14/06 13:49:58.009 ,TEST,6,456,456.7,45,4.56,453,34,54.3,3.2,6.456 (9 Replies)
Discussion started by: earlepps
9 Replies
Login or Register to Ask a Question