unix - Use values from an array, key as subtring of a column in awk -
i have file named order.csv, data
"company","new add date" "electrical insulation supplies","200212" "avis budget group","201110" "honeywell aerospace","201307" "avis budget group","201110" "merck sharp & dohme","199608" "pharma-bio serv inc","200803" "ups store","200407" "procter & gamble","200403" "w holding co inc","200712" "avis budget group","201110"
i want dates (last date of month) on basis of last 2 character of second column, use command:
awk -f, 'begin{a[01]="31";a[02]="28";a[03]="31";a[04]="30";a[05]="31";a[06]="30";a[07]="31";a[08]="31";a[09]="30";a[10]="31";a[11]="30";a[12]="31";}{ print $1, substr($2,2,6)a[substr($2,6,2)] }' order.txt
this giving output:
"company" new ad "electrical insulation supplies" 20021231 "avis budget group" 20111031 "honeywell aerospace" 201307 "avis budget group" 20111031 "merck sharp & dohme" 199608 "pharma-bio serv inc" 200803 "ups store" 200407 "procter & gamble" 200403 "w holding co inc" 20071231
which not extracting result, wrong i'm doing.
because number of days in february depends on whether year leap year or not, days per month depend on both month , year.
you can use following gawk
(gnu awk) script achieve that:
last_day.awk:
function days_per_month(year, month) { date = year" "month" 31 00 00 00" day = strftime("%d", mktime(date)) return 31-day%31 } # on every line of input { year = substr($2,2,4) month = substr($2,6,2) last_day = days_per_month(year, month) print $1, year""month""last_day }
call this:
gawk -f, -f last_day.awk order.csv
btw, gawk
specific because of use of mktime()
, strftime()
Comments
Post a Comment