I have code where Im going through each row at a time and taking the 3rd column which is the date.

It echos the date in format 04-04-2017. Each row has a different date and I wish to take 5 years from that date and put it in the new variable dob_want2 but keep getting the error date:

invalid date ‘'04-04-2017' -5 years’

The file temp9_0.txt is in the format

123 5555 04-04-2017
126 1234 25-11-2014
218 0023 13-06-2002

and echo "$dob_want" outputs


Its obviously not recognising $dob_want as a date. Any help suggestions on how I could treat this variable as a date would be much appreciated.

dob_want=$(head -1 temp9_0.txt | awk '{print $3}')
echo "$dob_want"
dob_want2=$(date  +%d-%m-%y -d "$dob_want -5 years")
echo "$dob_want2"
  • 3
    You will want to start by getting your date into a format that GNU date can parse. See e.g. here.
    – Kusalananda
    Jun 25, 2021 at 10:09
  • Are you trying to subtract dates? In a character format? Jun 25, 2021 at 10:35
  • 2
    For those wondering about the ‘ and ’ I suspect they come from inappropriate terminal translation of the message "ticks" rather then from the input data (I get the same using PuTTY with Win1252 instead of UTF-8). The real issue is the d-m-Y (or is it m-d-Y) format as @Kusalananda has already pointed out. Jun 25, 2021 at 11:59
  • @steeldriver I wonder if the ‘ and ’ are ticks that are included in the input data, which is wrongly encoded for the locale. (The OP including echo "$dob_want" as already asked could help ascertain that.) Jun 25, 2021 at 13:19
  • @roaima yeah AFAIK GNU date doesn't provide any flexibility about the input format - newer versions of busybox date do, as do the dateutils utilities ex. dateutils.dadd -i '%d-%m-%Y' -f '%d-%m-%y' 04-04-2017 -5y Jun 25, 2021 at 13:26

1 Answer 1


I am not sure if the date utility is required in your case at all, since you want to perform a rather simple operation on the year only (substracting 5 years would just mean decrement the yyyy part of dd-mm-yyyy by 5).

In this case, the following awk program will modify the dates in the third column of your input file:

~$ awk -v offs=-5 '{split($3,df,"-"); printf "%02d-%02d-%04d\n",df[1],df[2],df[3]+offs}' temp9_0.txt 

by adding the offset specified via as variable offs to the 3rd (dash-separated) sub-field of the 3rd column, and printing the modified subfield values via printf. It does so by using the split command to split the 3rd column at the - into the array df (date fields).

To apply this to a specific line rather than all, and assuming there are no empty lines, you can specify an additional variable line and make the program selective to only that line by evaluating FNR, awks automatic per-file line-counter:

awk -v line=3 -v offs=-5 'FNR==line {split($3,df,"-"); printf "%02d-%02d-%04d",df[1],df[2],df[3]+offs}' temp9_0.txt

This output could then be caught in a shell variable via command substitution, as in

dob_want2=$(awk -v line=3 .....)
  • Thank you- this worked as a work around!
    – dcp1234
    Jul 6, 2021 at 21:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .