1

I have this code where the cmd usually works if I sprintf something to it, but when I try to run my Rscript, it does not work. Any hints?

I get the error:

awk: cmd. line:9:         cmd = Rscript ./date-script-r.r $1 3 2 1;
awk: cmd. line:9:                       ^ syntax error
awk: cmd. line:9:         cmd = Rscript ./date-script-r.r $1 3 2 1;
awk: cmd. line:9:                         ^ unterminated regexp

Code:

awk=/usr/bin/awk

awkcommand='
#d is the delimiter
BEGIN { OFS = FS = d }

$1 {
    #Expected args for the Rscript: (1, 2, 3, 4) = (dateString, yearPosition, monthPosition, dayPosition)
    cmd = Rscript ./date-script-r.r $1 3 2 1;
    cmd | getline $1;
    print;
    close(cmd);
}

awk -v d="," "$awkcommand" output-data/$filename > output-data/tmp.csv

Example of R-script output:

Rscript date-script-r.r 17-12-12 1 2 3
12-12-2017

1 Answer 1

2

replace

cmd = Rscript ./date-script-r.r $1 3 2 1;

by

cmd = "Rscript ./date-script-r.r " $1 " 3 2 1" ;

for complex awk script it might be better to put them in a awk-script, e.g. date-awk.awk

$1 {
    #Expected args for the Rscript: (1, 2, 3, 4) = (dateString, yearPosition, monthPosition, dayPosition)
    cmd = "Rscript ./date-script-r.r " "$1" " 3 2 1";
    cmd | getline $1;
    print;
    close(cmd);
}

that you would call with

awk  -F, -f date-awk.awk  output-data/$filename > output-data/tmp.csv

note that

  • -F, will set , as separator, there is no need for a relay variable.
  • I expect this is part of a bigger scheme, or self tutorial. (there are easier way to compute date in shell or in awk).
1
  • Great thanks it works! I got help with the first part of the script using the date function (unix.stackexchange.com/questions/344794/…), but it does not meet the requirements for complex date auto detection. So yes, it is part of a larger and more complex script. Thanks for the help regarding running the Rscript. :-) I actually need to estimate where the days, months, and years, are when I have different input files with dates like 17-02-02 and 02-02-02. The gnu date function "fails" here.
    – Greg Nowak
    Mar 4, 2017 at 8:26

You must log in to answer this question.

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