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: add from two digits (the month) within date string dd/mm/yyyy

In this snippet I am using an exel formula mid() to illustrate what I need to do, but do not know the AWK code for it.

awk 'BEGIN {FS=","; OFS="->"} {newval=($1*12)+(mid($10,4,2); print $0,newval}' testdate.csv >out.csv

where mid($10,4,2) is extracting and using the 12 from text entered like this 01/12/2005 for example

Based on this (in fact on previous code shown to me in here, but looked up as well)

"substr(s, a, b) : it returns b number of chars from string s, starting at position a. The parameter b is optional."

is it substr ? So should it be

awk 'BEGIN {FS=","; OFS="->"} {newval=($1*12)+(substr($10,3,2); print $0,newval}' testdate.csv >out.csv

Clearly not as I get this:

MacPro:FinalProcess ADM$ awk 'BEGIN {FS=","; OFS="->"} {newval=($1*12)+(substr($10,3,2); print $0,newval}' testdate.txt >out.txt
awk: syntax error at source line 1
 context is
	BEGIN {FS=","; OFS="->"} >>>  {newval=($1*12)+(substr($10,3,2); <<< 
awk: illegal statement at source line 1
awk: illegal statement at source line 1
	missing )

Open in new window



maybe newval needs opening and closing brackets..
testdate.csv
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
Avatar of Anthony Mellor

ASKER

thanks for stating the obvious - which is to say obvious after I see your answer.