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
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 }
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"
Summary: Hi, I need to find max column lenth of each column in a file. for eg. if file has col1 col2 col3 xyz abc mnopq ab pq xx The output i need as col 1: 3, col2 :3...
Summary: Is there any way using the korn shell to delete all records in a file except one?? In other words if I had 6 lines in a file, could I issue one command and delete all rows execpt for row 3?? Any hel...
Summary: my problem is i want to replace a line in a file e.g i want to replace the line DBUID= (some name) with DBUID=$user variable file name is sample.ksh DBUID=aruns010 please help me in solving this pro...