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

Max column count in a file

Reply to Message Icon

Original Message
Name: sxk1774
Date: August 23, 2004 at 23:04:20 Pacific
Subject: Max column count in a file
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.


Report Offensive Message For Removal


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

No 'wc -L'?

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


Report Offensive Follow Up For Removal

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

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"


Report Offensive Follow Up For Removal

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

What about leading whitespace Jim? ;-)


Report Offensive Follow Up For Removal

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

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"


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Max column count in a file 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge