LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-09-2009, 04:31 PM   #1
Aveltium
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Rep: Reputation: 0
Talking [bash] 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.

Last edited by Aveltium; 04-09-2009 at 04:37 PM.
 
Old 04-09-2009, 04:47 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,451
Blog Entries: 54

Rep: Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894
Unless you really need the exercise, shouldn't you just do 'outputSomething|column -t' instead?
 
Old 04-09-2009, 04:58 PM   #3
Aveltium
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Original Poster
Rep: Reputation: 0
OMG! xD

I feel so silly now.

I wasted bunch of time on this for nothing..

Or maybe at least I've learned something.

Thank you very much for the quick responding!
 
  


Reply

Tags
fields, generate, table


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
Generating a Primary Key for a Table resetreset Linux - Server 11 09-07-2008 06:52 AM
Generating a Primary Key for a Table resetreset Programming 2 09-04-2008 12:03 PM
Bash: sorting by two fields humbletech99 Programming 7 09-25-2007 03:30 PM
MySQL: List fields of a table mikeshn Programming 2 06-17-2003 12:12 PM


All times are GMT -5. The time now is 12:00 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration