Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

strip leading zeros in awk program

So I'm pretty lost as I missed a week and am playing catch up, but I'm to write an awk program to tell the difference, in days, between two dates.

I'm more or less done with it, however it doesn't like it if the user inputs 01/01/YYYY when doing my data validation. I pull the 3 fields out of the arg then pass them to a function to validate.

the part that's giving me trouble is, in my validation:

if ( day > Days[month] ){
invalid stuff here
}

where Days[] is an array holding [1]=31, [2]=28 (or 29 if leap) etc.

It hits true because Days[01] is not defined. I could go around it by defining a [01] as the same as [1] I suppose but I'd rather something more elegant.

How can I strip the leading zeros, within the awk program, of the variable month? Everything I've found is for use in a shell script and invokes awk, but that doesn't really help me.

Thanks for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Another option:

$ echo 01234 | awk '{x=$0+0;print x}'
1234

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...