CharlesM
CharlesM

Reputation: 561

(g)awk: use GNU's date within system()

Inside a shell script, I want to modify a CSV file (with "|" as field separator) having one column (the eight) with dates as values.

More precisely, I want to increase each date by one day:

10/06/2018 becomes 10/07/2018

So, I'm trying to use GNU's coreutils 'date' command (in a Linux Mint environment) within gawk's system(), but my output is all wrong (just one column instead of the whole file and with one single value repeated).

awk -i inplace -F'|' -v OFS='|' '$8=system("date -d " $8+1days " +%m/%d/%y")' file.csv

Since, as justly pointed out in the comments, 'date' is not part of Bash but an external command, should I still be using system()?

Upvotes: 0

Views: 102

Answers (2)

karakfa
karakfa

Reputation: 67497

if you're using gawk you don't need a system call

$ awk -i inplace 'BEGIN {FS=OFS="|"} 
                  {split($8,a,"/"); 
                   $8=strftime("%m/%d/%Y", mktime(a[3]" "a[1]" "a[2]" 00 00 00")+24*60*60)}1' file

Upvotes: 3

chepner
chepner

Reputation: 531693

The +1 days needs to be part of the input to system, and as such, quoted. It also needs to be quoted at the shell level, too.

awk -i inplace -F'|' -v OFS='|' '$8=system("date -d \""$8" +1 day\" +%d/%m/%y")' file.csv

Upvotes: 0

Related Questions