Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Max Length Column Checks 1

Status
Not open for further replies.

tcerv79

Programmer
Mar 26, 2009
16
US
I have a flat file that is |(pipe) delimited and it has multiple records. I want to check each record and between all pipes for a preset amount of characters that I have define and have it fail if the characters exceed the check. Here is an example:

|THOMAS|CROWN|05/01/2009|123 MAIN STREET|...etc

WHERE the max characters are as follows for the matching column:

|(max length of 25)|(max length of 25)|(max length of 10)|(max length of 100)|....etc

I am unsure on where to even start with this. Any help would be awesome. I have found some question that are similar, but to no success.
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Try this:

Code:
awk -F'|' '
        BEGIN {
                maxwidth[1]=0
                maxwidth[2]=25
                maxwidth[3]=25
                maxwidth[4]=10
                maxwidth[5]=100
                maxwidth[6]=10
        }
        {
                for (i=1; i<=NF; i++) {
                        if (length($i) > maxwidth[i])
                                print "field "i" of record "NR" is too wide (width " length($i)" max " maxwidth[i] "): " $i
                }
        }
' inputfile

Annihilannic.
 
Annihilannic....Never even thought, or knew really about maxwidth. I will definitely do that. It appears like it will work as I expect. I was looking at defining the LENGTH and setting it equal to 1 and when it was not equal returning the error, but this was in theory. You always have great answers.
 
maxwidth isn't a feature of awk that you didn't know about... it's just the name I gave to an array containing the widths.

Annihilannic.
 
Ha..of course you did, and now that I am trying it out I see that. Thanks again and my apologies for my lack of awk knowledge.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top