1

I have a file called days.txt that stores the no. of days. It consists of only one record and at the moment it's 7.

I want to use the value of days.txt in the below awk expression

awk -F, '{ $1 = substr($1,1,1) strftime("%Y%m%d",mktime(sprintf("%4d %.2d %.2d 00 00 00",substr($1,2,4),substr($1,6,2),substr($1,8,2)))-7*24*60*60) }; 1' OFS="," file

This awk statement is basically shifting the date from another file by 7 days.. When I try to use the value of days.txt from a variable, awk statement does not return any error but it does not shift the date as well.

awk -F, '{ $1 = substr($1,1,1) strftime("%Y%m%d",mktime(sprintf("%4d %.2d %.2d 00 00 00",substr($1,2,4),substr($1,6,2),substr($1,8,2)))-$(value)*24*60*60) }; 1' OFS="," file

Any help on this would be very helpful!!

steeldriver
  • 81,074

1 Answers1

3

Don't read the value into a shell variable first, just read it into an awk variable:

awk '
    BEGIN {FS=OFS="," }
    NR==FNR { value=$0; next }
    { $1 = substr($1,1,1)...substr($1,8,2)))-value*24*60*60) }
1' days.txt file
Ed Morton
  • 31,617