0

My CSV file has 47 columns, and I want to select columns 36 - 47 by the values of a certain field. Starting column 36 to 47, each entry is like this: "1/1:0,297:297:99:10476,951,0"

I use the following AWK code to split this entry by ":", and check if array[3] >= 20 (the above highlighted is 297 in array[3]. If it passes the check, print out the entry to the new CSV, otherwise print out "./.". I need to print out the first 35 columns without condition.

When I run the following code in a file named awk_byDP (chmod u+x), it gives me this error:

/usr/bin/awk: syntax error at source line 6 source file ./awk_byDP context is

? <<< /usr/bin/awk: illegal statement at source line 6 source file ./awk_byDP

#!/usr/bin/awk -f

BEGIN {-F","; OFS=","}
 NR <= 1 {next}
 NR > 1 {
     for (j=1; j<=35; j++) { printf("%s",  $j) } #line 6  

     for (i=36; i<=47; i++) {
     t=$i;
     split(t,a,":")
     if ( a[3]>=20 ) {
            printf(“%s”, $i)
       }  
       else {
          printf(“%s”, "./.")
       }
     }
    printf("\n")
}
2
  • Didn't generate any error for me! Tested on compileonline Commented Oct 31, 2013 at 18:07
  • what OS are you running this under? Does awk --version return anything helpful? Good luck. Commented Oct 31, 2013 at 22:05

2 Answers 2

2

Your syntax error is in the BEGIN section. Change this:

BEGIN {-F","; OFS=","}

to this:

BEGIN {FS=OFS=","}

You have a couple of minor, mostly cosmetic/style issues too, here's a cleaned up version of your script:

BEGIN { FS=OFS="," }
NR > 1 {
    for (j=1; j<=35; j++) {
        printf "%s",  $j
    }  

    for (i=36; i<=47; i++) {
        split($i,a,/:/)
        if ( a[3]>=20 ) {
            printf "%s", $i
        }  
        else {
            printf "./."
        }
    }
    print ""
}
Sign up to request clarification or add additional context in comments.

Comments

0

Change to :

 for (j=1; j<=35; j++) { printf "%s",  $j }

3 Comments

That'd be preferable but it's not a syntax error to use parens around the printf args,
Sorry. I see a function call with no assign and automaticaly think its erroneous. I've tried your code (gnu awk, and mawk) and the only problem with it are the quotes. They are not standard double quotes.
Well spotted - I just copy/pasted the OPs code, never dreamed there'd be some odd quotation marks in there! I've updated my answer. I assume that's what the point of your answer was, then, that the quotes had to change. It looked like you were just saying to remove the brackets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.