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

length in if statement

Status
Not open for further replies.

mtn17

Programmer
Mar 6, 2009
4
US
I am new to AWK....I have these lines of codes, tried to run and received error but didn't understand the error msg.

awk '
var1 = length($3)
if (var1 = 6)
nawk -F, '{OFS=","; print $1,$2,"00"$3" ",$8}' /tmp/payfile > /tmp/paywork1

if (var1 = 5)
nawk -F, '{OFS=","; print $1,$2,"000"$3" ",$8}' /tmp/payfile > /tmp/paywork1

'

Please help!
 
A starting point:
Code:
nawk -F, '{printf "%s,%s,%08d  ,%s\n",$1,$2,$3,$8}' /tmp/payfile > /tmp/paywork1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for replying. So if I have another line like this:

nawk -F, '{OFS=","; print $1,$2,"00"$3" ",$4" PH",$6," PAYROLL HOURS USD ",$5,$7,$8}' /tmp/payfile > /tmp/paywork1

The same code can be written as this (based on your logic):

nawk -F, '{printf "%s,%s,%08d" ",%s, PH",%s,"" PAYROLL HOURS USD ",$1,$2,$3,$4,$6,$5,$7,$8}' /tmp/payfile > /tmp/paywork1

Ultimately, I want $3 to be a max length of 8
 
Look at the printf function in your awk manual.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PVH,

original code:
nawk -F, '{OFS=","; print $1,$2,"00"$3" ",$4" PH",$6," PAYROLL HOURS USD ",$5,$7,$8}' /tmp/payfile > /tmp/paywork1

new (I still need help on what to do with inputs ($5,$7,$8)

nawk -F, '{printf "%s,%s,%08d ,%s PH,%s, PAYROLL HOURS USD \n",$1,$2,$3,$4,$6}' /tmp/payfile > /tmp/paywork1

note: the main reason for this change because I want $3 to be 8 char long with padded 0s
 
Thanks PHV and vgersh99 for all your help!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top