Setting the delimiter

Hello all,

How can I set TAB as the delimiter inside a script file that will manipulate another file? For example, let's say I have a file with four fields separated by tabs and each field has four items. Can I set TAB as a delimiter inside a script file or can I only do that at the shell?

Thanks for any help!

Trellot

this might be of help to you

The link only brings me back to mine. Any suggestions?

Trellot

IFS is the important delimiter variable. Not sure how you set it to being a tab though. Used for "read X Y Z" etc.

You can set the shell variable IFS to tab

nice goof up, i meant to point here

Ok, so I think I have the tab thing figured out...only thing is now that I can't seem to align the fields along the left side due to one word being quite longer than the others in the first field, for example:

Giants 2007 Finished last place
Reds 2007 Finished fifth place
Philadelphia 2007 Finished first place

Something like this...so everytime I increase the control characters (%s) to align the Philadelphia line stays out of alignment increasing as well. What can I do here?

Thank you,

Trellot

To me that counts as part of the "presentation layer" and I would keep that separate from the data manipulation.

I would have thought you would want the data independant of the number of tab-stops in whatever display mechanism you use.

I'm good so far, thanks everyone for the info about the tabs. Now I'm trying to calculate the average of a particular field. I know tot(and then the field name)+= will total it, but what about an average?

Trellot

Also, rest assured that I'm not just throwing all of this on all of your good shoulders. I, too, am googling and the like for answers. Just so you all know :slight_smile:

Trel

Er, total elements then divide by number of elements?

Right, but I'm not figuring out how I divide by 4 (the number of elements) syntactically. So, like I have this:

......
{ printf("8s%15s\n", $1, $4); totRBIs+=$1; avgRBIs/4=$4 }

END { printf("Total RBIs = %8.f\n", totRBIs)
printf("Average RBIs = %8.f\n", avgRBIs)

I'm not sure of the mathematical syntax here....

Trel

Figured it out...thanks.

Trellot