Reputation: 203
I have a data file contain the following columns
Current Date FirstName LastName
8/31/2014 AAA BBB
8/31/2014 CCC DDD
What I want to do is extract the date in the CurrentDate column, convert it and put it into a new column with just the year and the month.
Like this:
CurrentMonth CurrentDate FirstName LastName
201408 8/31/2014 AAA BBB
201408 8/31/2014 CCC DDD
I tried with awk command
awk -F $'\t' 'BEGIN {OFS=FS} { { split($1, val,"/") } print val[3] val[1],$0}' > outputFile
However the above command gives me 20148 rather than the 201408.
CurrentMonth CurrentDate FirstName LastName
20148 8/31/2014 AAA BBB
20148 8/31/2014 CCC DDD
How do I pad 0's into the date?
Upvotes: 1
Views: 72
Reputation: 45243
with printf
function in awk, use %02s
Here is the code
awk 'BEGIN{print "CurrentMonth CurrentDate FirstName LastName"}
NR>1{split($1,a,"/"); printf "%s%02s\t\t%s\n",a[3],a[1],$0}' infile
CurrentMonth CurrentDate FirstName LastName
201408 8/31/2014 AAA BBB
201408 8/31/2014 CCC DDD
Upvotes: 0
Reputation: 179422
Just use printf
:
awk -F $'\t' 'BEGIN {OFS=FS} { { split($1, val,"/") } printf "%04d%02d\t%s\n", val[3], val[1],$0}'
printf
in awk works much the same as printf
in C; you can find the specifications for the format language on any C reference site (e.g. here). Briefly, in a format specification like %04d
, %
introduces a format operator, 0
means pad with zeros, 4
means the width to print out, and d
means "print as a decimal number".
This awk script yields
201408 8/31/2014 AAA BBB
201408 8/31/2014 CCC DDD
Upvotes: 1