An AWK question

ok, hands up im a bit of a noob when it comes to shell scripting but I am stuck on some AWK type problems that i wonder if you can help me with

My file, comma delimited, looks like this

00004290.00,000000.00
00233238.43,000000.00
00000122.67,000000.00

as you can see i have two fields $1 and $2 . I have two questions

1) in $1 (the amount field in my case), i would like to remove the leading zero's (regardless of how many there are) I realise that this will make the file no longer a fixed length file but that is no problem, as its being used for a mail merge.

2) on a slightly different note, on $2 (the outstanding balance field in my case) I need to remove the leading zeros, unless the value (as above) is actually at zero, as obviously removing the leading zeros from 000000.00 doesnt really give the desired effect.....i would like it to be the value of just "0" or "0.00" if this is the case.

any pointers or guidance on this would be greatly appreciated
Gary

BEGIN {
  FS=OFS=","
}
{
  gsub(/^0*/, "", $1)
  gsub(/^0*/, "", $2)
  gsub(/^[.]/, "0&", $2)

  print
}

thanks for that, I havent got it working yet but if i were to organise that onto a single line in a script would it be like this (ps this layout doesnt work hence the post :wink: )

awk 'BEGIN {FS=OFS=","} { gsub(/^0*/, "", $1) gsub(/^0*/, "", $2) gsub(/^[.]/, "0&", $2) print}' inputfile > outputfile

i get

awk: syntax error near line 1

Well for one thing, each awk command needs a semicolon when combined on one line:

gsub(/^0*/, "", $1); gsub(/^0*/, "", $2); gsub(/^[.]/, "0&", $2); print

  1. . ↩︎

Ok, i tried and still no joy :confused:

awk 'BEGIN {FS=OFS=","} { gsub(/^0*/, "", $1); gsub(/^0*/, "", $2); gsub(/^[.]/, "0&", $2); print}' inputfile > outputfile

Then you're probably like me, I use nawk rather than awk. Your script works for me using nawk.

wow youre right it works fine with nawk, ill use that then ....thanks all for your input :smiley: