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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using AWK to pad empty fields 1

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
0
0
GB
Hi All,
I am using AWK to parse lines of a logfile to extract pertinent data but have hit a problem that I'm not even sure I can solve with AWK.

Within the logline I have three bracketed sections; the first of which are the fixed fields. i.e There are always seven values. The second and third sections are the corresponding values to each fixed value. So in the logline example below,
First fixed value 1 has values ranging from 1.2 to 1.4 (first fields in sections two and three) and so on.
Second fixed value 2 has values ranging from 1.3 to 1.5.
Third fixed value 3 has values ranging from 1.4 to 1.6.

[1,2,3,5,10,15,20],[1.2,1.3,1.4],[1.4,1.5,1.6]

My issue is that i need to pad the second and third fields with 0.0 if no match to the fixed fields in section 1.

So, using the above, [1,2,3,5,10,15,20],[1.2,1.3,1.4,0.0,0.0,0.0,0.0],[1.4,1.5,1.6,0.0,0.0,0.0,0.0]

Clearly i'm not an expert and to be honest cannot even think of a way to do it, apart from possibly using a for loop and an associative array for section one and pumping 0.0 for any null array values.

Any help or pointers in the right direction would be most appreciated.

Regards


 
Something like this ?
Code:
awk -F '[][]' 'NF==7{
s1=$2;s2=$4;s3=$6
n=split(s2,a,",");for(i=n;i<7;++i)s2=s2",0.0"
n=split(s3,a,",");for(i=n;i<7;++i)s3=s3",0.0"
print "["s1"],["s2"],["s3"]"
}' /path/to/logfile

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you PHV, thats a great solution. Let me try it out.

Really appreciate the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top