[bash scripting] Generating a table with aligned fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash scripting] Generating a table with aligned fields
# 1  
Old 04-09-2009
Computer [bash scripting] Generating a table with aligned fields

Hi everyone,

I want to write a function which calculates the space needed between fields, to generate a table with aligned fields, like when you type "ls -l", the operating system generates a table with beautifully aligned fields.

I've got this code so far:

Code:
for line in $(cat tmpSearch)
do
    line=`echo $line | tr ":" " "`
    x=0
    for field in $line
    do
        x=`expr $x + 1`
        lineLength=`eval \`echo "gawk -F: '{print \\\\$\$x}' tmpSearch | gawk '{print length}' | sort -n | tail -n 1"\``
        fLine[$x]="`eval \`echo \"echo \$field | gawk '{printf \\"%-\"$lineLength\"s\\\\\n\\",\\\\$0}'\"\``"        
    done
    echo "${fLine[@]:1}"
done

Original file: tmpSearch
Code:
000001:Mia:Miki:19871221:20090401:20471221:SSK:7000:7700
000002:Mia:Sean:19800101:20090401:20500101:SK:3000:3210
000003:Mia:Boo:19641020:20090401:20241020:SK:7000:7490
000004:Mia:Nya:19921123:20090401:20521123:SSK:7000:7700
000005:Mia:Nya:19921123:20090405:20521123:SK:7000:7490
000006:Mia:Nya:20001010:20001210:20601010:SSK:400000:440000
000007:Mia:Nya:20051010:20061010:20651010:HSK:2000:2160
000008:Mia:Nya:19871221:20090401:20471221:SSK:5000:5500
000009:Mia:Nya:19800101:20090401:20500101:SK:3000:3210
000010:Mia:Miki:19871221:20090401:20471221:SSK:7000:7700
000011:Mia:Miki:19871221:20090401:20471221:SSK:7000:7700
000012:Mia:Miki:19800101:20090401:20500101:SK:7000:7490
000013:Mia:Miki:20001010:20041010:20601010:HSK:7000:7560
000014:Mia:Miki:19800101:19801011:20500101:SK:70000:74900

Result:
Code:
000001 Mia Miki 19871221 20090401 20471221 SSK 7000   7700
000002 Mia Sean 19800101 20090401 20500101 SK  3000   3210
000003 Mia Boo  19641020 20090401 20241020 SK  7000   7490
000004 Mia Nya  19921123 20090401 20521123 SSK 7000   7700
000005 Mia Nya  19921123 20090405 20521123 SK  7000   7490
000006 Mia Nya  20001010 20001210 20601010 SSK 400000 440000
000007 Mia Nya  20051010 20061010 20651010 HSK 2000   2160
000008 Mia Nya  19871221 20090401 20471221 SSK 5000   5500
000009 Mia Nya  19800101 20090401 20500101 SK  3000   3210
000010 Mia Miki 19871221 20090401 20471221 SSK 7000   7700
000011 Mia Miki 19871221 20090401 20471221 SSK 7000   7700
000012 Mia Miki 19800101 20090401 20500101 SK  7000   7490
000013 Mia Miki 20001010 20041010 20601010 HSK 7000   7560
000014 Mia Miki 19800101 19801011 20500101 SK  70000  74900

My code seems not good that it takes too much time to process.
Please help me to make it better.

I'm very new to shell scripting and trying to learn. If possible, please explain the meaning of the code.

I'm sorry for my bad English.

Any help is much appreciated.


** Edit: I feel stupid. Someone told me that I just need to simply use the "column -t" to accomplish this task. Thank you if anyone wanted to help me, and sorry for being stupid. Smilie

Last edited by Aveltium; 04-09-2009 at 08:14 PM..
# 2  
Old 04-10-2009
this should work fast..
Code:
no=`awk -F":" -v l=\`awk 'END{print NR}' filename\` '
{A[NR]=length($1)" "length($2)" "length($3)" "length($4)" "length($5)" "length($6)" "length($7)" "length($8)" "length($9)}
END{for(i=0;i<l;++i)
{split(A[i],B," ");
{if(B[1]>max1){max1=B[1]}}
{if(B[2]>max2){max2=B[2]}}
{if(B[3]>max3){max3=B[3]}}
{if(B[4]>max4){max4=B[4]}}
{if(B[5]>max5){max5=B[5]}}
{if(B[6]>max6){max6=B[6]}}
{if(B[7]>max7){max7=B[7]}}
{if(B[8]>max8){max8=B[8]}}
{if(B[9]>max9){max9=B[9]}}
}{print max1" "max2" "max3" "max4" "max5" "max6" "max7" "max8" "max9}}' filename`
awk -F":" -v var="$no" '{split(var,A," ")}{printf "%-"A[1]"s %-"A[2]"s %-"A[3]"s %-"A[4]"s %-"A[5]"s %-"A[6]"s %-"A[7]"s %-"A[8]"s %-"A[9]"s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9}' filename

# 3  
Old 04-11-2009
Thanks for responding! Smilie

Actually I need it to be more general, I mean not just to be running on 9 columns, but any given number of columns. But anyways I have learned something nice in your code. Thanks a lot! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Forum Support Area for Unregistered Users & Account Problems

Creation of Oracle table through shell scripting

Hi, I am trying to create a table through shell scripting . I used a command sqlplus -s to connect to sqlplus and user as sysdba. but there is a problem in it. can anyone please solve this . #!/bin/bash $(`sqlplus -s / as sysdba <<eof create table sample (id,int); insert into sample... (1 Reply)
Discussion started by: Unregistered
1 Replies

2. UNIX for Advanced & Expert Users

How can i populate the record in to table using shell scripting?

LOG_DIR=/app/rpx/jobs/scripts/just/logs sendEmail() { pzCType="$1"; pzTitle="$2"; pzMsg="$3"; pzFrom="$4"; pzTo="$5"; pzFiles="$6"; pzReplyTo="$7" ( echo "From: $pzFrom\nTo: $pzTo\nSubject: $pzTitle${pzReplyTo:+\nReply-To: $pzReplyTo}" ] && echo... (1 Reply)
Discussion started by: ankit.mca.aaidu
1 Replies

3. Programming

Celisus & fahrenheit table converter with fields + increments

no longer needed, please delete thread. (0 Replies)
Discussion started by: xtina
0 Replies

4. Homework & Coursework Questions

Table Scripting

1. The problem statement, all variables and given/known data: I have to make a manufacturing company. I need subdirectories for the company using three departments: Human Resources, Manufacturing, and Sales. I also need three scripts in order to create tables for each sub directory. Each script... (11 Replies)
Discussion started by: Catori
11 Replies

5. Shell Programming and Scripting

Table Scripting

Okay. I've been charged with an assignment to create tables. I "Made" a manufacturing company called "Suit and Tie". I need subdirectories for the company, using three departments: Human resources, Manufacturing, and Sales. 1) mkdir Suit_and_Tie 2) cd Suit_and_Tie 3) mkdir Human... (3 Replies)
Discussion started by: Catori
3 Replies

6. Shell Programming and Scripting

Converting date/time and generating offsets in bash script

Hi all, I need a script to do some date/time conversion. It should take as an input a particular time. It should then generates a series of offsets, in both hour:minute form and number of milliseconds elapsed. For 03:00, for example, it should give back 04:02:07 (3727000ms*) 05:04:14... (2 Replies)
Discussion started by: emdan
2 Replies

7. Shell Programming and Scripting

Novice in shell scripting - generating report

Hi I recently joined a project where I have been asked to generate a report using shell script accessing UNIX box. I have no idea on how to do it as I am a beginner and learning shell scripts. Suppose I have a XML: <XYZRequest> <effectiveDate>someDate</effectiveDate>... (2 Replies)
Discussion started by: vat1kor
2 Replies

8. Shell Programming and Scripting

generating report in Excel(Open office) using shell scripting

Hello All, I need to execute around 15 queries after which all data should come in Excel format. Executing 15 queries is not a problem. Problem is how to present/put data in excel. kindly suggest me how to start, what to study or what should i do. thanks, (1 Reply)
Discussion started by: shubham8787
1 Replies

9. Shell Programming and Scripting

Create a table using shell scripting

Hi Can we create a rectangular table as i have attached in the query . This is primarily for populating the created table with data gathered . Hope I made myself clear ... Pls suggest Thanks (1 Reply)
Discussion started by: ultimatix
1 Replies

10. Shell Programming and Scripting

Bash Scripts - File generating

Forgive the daft requests - I'm still a learner :D I need a script so that I can test another script (I'm confused already) The script I am looking for should generate a new file in the same directory (called newfile1 or what ever) and also generate text within the new file (Hello world? What... (1 Reply)
Discussion started by: JayC89
1 Replies
Login or Register to Ask a Question