[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
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.
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!
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)
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)
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)
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)
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)
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)
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)
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)