Computing.Net > Forums > Unix > Max column count in a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Max column count in a file

Reply to Message Icon

Name: sxk1774
Date: August 23, 2004 at 23:04:20 Pacific
OS: AIX
CPU/Ram: 2 GB
Comment:

I have to send a file to mainframe and before sending it, I have to execute the quote command to set the record length.
Since the file is dynamic, I do not know what the maximum size of a line could be.

Currently, I use the following function to get the Max Column Count. Since I use "sed" it takes a long time for big files.

GetMaxCols()
{
MAX_COL_SIZE=0
for line in `cat $1 | sed "s! !~!g"`
do
COL_COUNT=`echo $line | wc -c`
if [ "$COL_COUNT" -gt "$MAX_COL_SIZE" ]; then
MAX_COL_SIZE=$COL_COUNT
fi
done
echo $MAX_COL_SIZE
}


Any better ways to achieve this goal ?

Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: Wolfbone
Date: August 24, 2004 at 06:16:07 Pacific
Reply:

No 'wc -L'?

awk '{if (length()>max) max=length()}; END {print max}'


0

Response Number 2
Name: Jim Boothe
Date: August 24, 2004 at 07:08:17 Pacific
Reply:

I prefer the awk solution as well. But if you used a shell loop, the following ksh code should be very efficient (and it handles multiple embedded spaces, so you don't need to mass change them to something else).

maxlen=0
while read line
do
len=${#line}
((len>maxlen)) && maxlen=$len
done < $1
echo "maxlen=$maxlen"


0

Response Number 3
Name: Wolfbone
Date: August 24, 2004 at 13:30:40 Pacific
Reply:

What about leading whitespace Jim? ;-)


0

Response Number 4
Name: Jim Boothe
Date: August 24, 2004 at 14:41:33 Pacific
Reply:

Drat! Thanks for pointing that out Wolfbone.

That solution needs one extra command:

IFS=
maxlen=0
while read line
do
len=${#line}
((len>maxlen)) && maxlen=$len
done < $1
echo "maxlen=$maxlen"


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


more newbie question .cshrc file and setting u...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Max column count in a file

Max column count in AIX www.computing.net/answers/unix/max-column-count-in-aix/7958.html

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html

how to replace a line in a file www.computing.net/answers/unix/how-to-replace-a-line-in-a-file/7214.html