strip leading zeros in awk program

后端 未结 5 1505
孤城傲影
孤城傲影 2020-12-19 20:28

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 les

5条回答
  •  臣服心动
    2020-12-19 21:11

    You need this, assuming your variable is called month:

    gsub ("^0*", "", month);
    

    The ^ is the start anchor and 0* means zero or more 0 characters. So this effectively removes all 0 characters at the start of the variable.

    By way of example (which also shows a way to do it to the middle number as well, before splitting the date apart, see the second gsub for that):

    pax> echo '1/1/2013
    03/12/2014
    02/02/1965' | awk '{gsub ("^0*", "", $0); gsub ("/0*", "/", $0); print}'
    
    1/1/2013
    3/12/2014
    2/2/1965
    

提交回复
热议问题