Link to home
Start Free TrialLog in
Avatar of Anthony Mellor
Anthony MellorFlag for United Kingdom of Great Britain and Northern Ireland

asked on

AWK: digits < 10 add a zero how?

AWK 'BEGIN{FS=","; OFS=","} {print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$12":"($35<10,0)$35,$38,$39}' 7aDATslashdatesaddedto36.txt>8aDATsTimeAdded.txt

Open in new window


Column $35 is the day of the month number, 1 to 31. Days less than 10 I need to add a zero. How?
I'm not ready for regex...

awk only please.
Avatar of Bill Prew
Bill Prew

You can use the format specification in the printf() or sprintf() to do this.  So a format of %.2d Would always print a value as two digits and will pad on the left with zero to get to that length.  Try this small example.

awk 'BEGIN{n=2;printf("%.2d\n",n);printf("%.5d\n",n)}'

Open in new window


if that doesn't work you may have to escape the backslashes, so you can try:

awk 'BEGIN{n=2;printf("%.2d\\n",n);printf("%.5d\\n",n)}'

Open in new window

~bp
Lots to read here, but the printf (and sprintf) are quite powerful, so worth studying on.

https://www.gnu.org/software/gawk/manual/html_node/Printf.html#Printf


~bp
Avatar of Anthony Mellor

ASKER

evening Bill,

that mean I can add an f to my print so printf and format just $37 with "%.2d ??

never sure what that \n means..

I was about trying this:

 AWK 'BEGIN{FS=","; OFS=","}(IF $35<10) {print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$12":0"$35,$38,$39}:{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$12":"$35,$38,$39}' 7aDATslashdatesaddedto36.txt>8aDATsTimeAdded.txt
AWK: syntax error at source line 1
 context is
	BEGIN{FS=","; OFS=","}(IF $35<10) {print >>>  $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$12":0"$35,$38,$39}: <<< 
AWK: bailing out at source line 1

Open in new window


so I have removed the colon it's working, not sure what it's doing yet. No, it output the file but did not add any required 0s

Anthony

so %.2d is print precision to digits integer
I've noticed you always use printf and iirc that means it has to be told when to send a newline, which I'm thinking I recall is the \n
You would need to replace the print() statement with a printf() statement, and then specify the format of the output line first, followed by each field.

Yes, print() adds a linefeed after printing, printf() doesn't, that's why we add the \n to then of the format string of the printf(), so that we add a linefeed at the end of the line.

~bp
From reading the manual  I must admit I don't relish formatting 39 fields just for the sake of one. How about a conditional print like the one I am battling with here:

the only difference in these is $37 plus or not one zero

awk 'BEGIN{FS=","; OFS=","}{if($35 <10) {print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$12":0"$35,$38,$39 } else print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$12":"$35,$38,$39}'

Open in new window


I'm hoping NR!=1 will prevent damage to row 1.
You can still use print if you want, and use sprintf() to format just the one field you want to format, that would work and be a lot easier.

~bp
I was looking for that, how to format one field,
This has worked, but I suspect sprintf would be a much cleaner solution if it can format only one field.

awk 'BEGIN{FS=","; OFS=","}{if($35 <10) {print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$12":0"$35,$38,$39 } else print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$12":"$35,$38,$39}' 7aDATslashdatesaddedto36.txt >8aDATsTimeAdded.txt

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the colon is because $12 and $35 together are a time e.g. 16:07
Thanks