Max column count in a file
|
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 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 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: