简体   繁体   中英

How to pad dates with 0's in a data file?

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?

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 (eg 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 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM