Anthony Mellor
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
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.
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
https://www.gnu.org/software/gawk/manual/html_node/Printf.html#Printf
~bp
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:
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
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
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
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
ASKER
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
I'm hoping NR!=1 will prevent damage to row 1.
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}'
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
~bp
ASKER
I was looking for that, how to format one field,
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
the colon is because $12 and $35 together are a time e.g. 16:07
ASKER
Thanks
Open in new window
if that doesn't work you may have to escape the backslashes, so you can try:
Open in new window
~bp