arrays and needing length of fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting arrays and needing length of fields
# 1  
Old 05-22-2008
arrays and needing length of fields

I have a sort of complex problem that I just can't figure out. I have data coming into a ksh program in a format that I need to parse out and display into a different format into a text file for printing. I have figured out how to get all the data in the format I need it in for the text file. The problem I am having is getting the columns to line up. They all need to be right justified and be as big as the max length of the data in each column. I won't know that until all the data is in the array. I don't know if I am explaining it well. I am going to try with a picture.

Col1 Col2 Col3 Col4 Col5
12345 12345 44 0 234
222222222 444 0 4444444 666666888999

This is how the data is coming out now. I am going to put it into an array, but have not done this yet. I don't know the length of each of the rows until I get the data.

So, each row is a separate column with its own length. I have just put spaces in between each column for now. I need it to look like this:

Col1 Col2 Col3 Col4 Col5
12345 12345 44 0 234
222222222 444 0 4444444 666666888999

As you can see I don't know what is the max length of each column until all the data is in. I need it to be right justified. I have no idea how to do this.

Is this a good explanation? I hope someone can help me. Thank you so much.

Allyson
# 2  
Old 05-22-2008
use printf
# 3  
Old 05-22-2008
I was printing out each line as it came in and I know I can't do that since I don't know the length. So, if I put it all into an array like this:

array[0]=first row with all columns separated by a space
array[1]=second row with all columns separated by a space
.
.
.

Then when I want to print it, how do I get each individual column out? Is there such a things as a two-dimensional array in ksh? Is so, I think that would be my best bet.

I am keeping the max length in a sort of hash with the column as the key and getting the length of each value and overwriting with the max one.

If there is a 2 dimensional array in ksh, that might solve my problems.

Can I have an example of the printf statement you were talking about?

Thanks.

Allyson
# 4  
Old 05-22-2008
input file

Code:
$ cat prinfdata
12345 12345 44 0 234
222222222 444 0 4444444 666666888999


Code:
awk '{ printf("%-12s %-6s %-3s %-12s %-25s\n", $1, $2 , $3, $4, $5)}' prinfdata

output


Code:
12345        12345  44  0            234
222222222    444    0   4444444      666666888999

# 5  
Old 05-22-2008
I can't seem to make it look like I want it to here. I am going to do each column separately on a new line to show what I need, but really they are all together with Col1, Col2, Col3, Col4, Col5 being on the same row and so on with each row. They are all right justified so they appear on the way right side of the page.

Col1
12345
222222222

Col2
12345
444

Col3
44
0

Col4
0
4444444

Col5
234
666666888999
Does this make anymore sense?

Thanks.

Allyson
# 6  
Old 05-22-2008
can u pls show ur script
# 7  
Old 05-22-2008
It is a bit confusing, but here is the part that I am trying to figure out:

while [[ $i -le $meascount ]];do
firstpass="${meascolumn##<meas-column>}"
meascol="${firstpass%%</meas-column>}"
if [[ $meascol = $i ]];then
firstpass="${measvalue##<meas-value>}"
measval="${firstpass%%</meas-value>}"
measvallen=${#measval}
(( measvallen>maxlen )) && maxlen=$measvallen
typeset mlen_${meascol}=$maxlen

if ((${finaldaytime}<${finalstarttime})) ; then
earlydate=$earlydate$meascol$colon$measval$dash
typeset val_${meascol}=$measval
i=$(($i+1))
break
fi
measlen=${#measval}
collen=$(eval echo \$mlen_${i})
(( spaces=collen-measlen ));
typeset -L$spaces colfill=" "
* eachlinevals="${eachlinevals}$space${measval}${colfill}"
read meascolumn
if [[ $meascolumn = "</tlm-meas>"* ]];then
while [[ $i < $(($meascount-1)) ]];do
i=$(($i+1))
* eachlinevals="${eachlinevals}$space$dash"
fi
done
break
fi
read measvalue
read limitsflag
else
* eachlinevals="${eachlinevals}$space$dash"
fi
fi
i=$(($i+1))
done

This without an array yet. I was trying to do it line by line first, but that doesn't seem to work. I tried to give the most relevant pieces of code, but there is much, much more. Thanks.

Allyson
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the file based on two fields - Fixed length file

Hi , I am having a scenario where I need to split the file based on two field values. The file is a fixed length file. ex: AA0998703000000000000190510095350019500010005101980301 K 0998703000000000000190510095351019500020005101480 ... (4 Replies)
Discussion started by: saj
4 Replies

2. Shell Programming and Scripting

File splitting according to the length of the fields

Hi All, I have two files: 1> Data file 2> info file which has field lengths and start position. Is there a way to create a comma delimited file according to the fields length and start position. Data file : R-0000017611N-00000350001095ANZU01 A00000017611N000000350001095ANZU02... (11 Replies)
Discussion started by: nua7
11 Replies

3. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

4. UNIX for Dummies Questions & Answers

Using the Foreach loop, Needing help

I am trying to make a script for my Counter-Strike: Source servers. What i am wanting it to do is for it to restart each server, the only way i can think of doing this in through for each. Years what i have at the moment. server_start() { START=`ps x | grep SCREEN | grep $SRV | cut -d '?' -f... (5 Replies)
Discussion started by: grahamn95
5 Replies

5. Shell Programming and Scripting

Fixed length fields

HPUX and posix shell Hi all. I have a record with fixed length fields....I would like to reorder the fields and preserver the fixed lengths.... cat test 4 960025460 Dept of Music 8 960025248 Dept of Music 12-08 cat... (3 Replies)
Discussion started by: lyoncc
3 Replies

6. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

7. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

8. Shell Programming and Scripting

Need awk script to compare 2 fields in fixed length file.

Need a script that manipulates a fixed length file that will compare 2 fields in that file and if they are equal write that line to a new file. i.e. If fields 87-93 = fields 119-125, then write the entire line to a new file. Do this for every line in the file. After we get only the fields... (1 Reply)
Discussion started by: Muga801
1 Replies

9. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies

10. Shell Programming and Scripting

fixed length fields in awk

I am trying to display df -h command out in proper format, how can I display each field of each record in a fixed length. (2 Replies)
Discussion started by: roopla
2 Replies
Login or Register to Ask a Question