I have run the following command on the command line and it works fine, but when I try running it from a shell script and run shell script from the terminal I get the following error:

join -t $'\t' -a1 -e "u" -1 1 -2 1 -o 1.1,2.1,2.2 file1 \ <(cat file2 | sort -k1,1|tr [:blank:] \\t ) | awk 'BEGIN {FS="\t";OFS="\t"}{if \($2 == "u")print $0, $1;else print $0, $3}' | awk '{gsub(/ /,"\t");l=$4; sub(/.*_/,"",l); print $2"\t"$3"\t"l}' > out

syntax error near unexpected token('`

I have tried escaping the bracket but it still gives me the same error. I have many of these multi-layered commands in the script so I am sure this will be a problem in other parts, is there a way to solve this?

  • 3
    And I thought badly written perl was read-only.... – roaima Dec 4 '15 at 13:26
  • running that code gives me different errors (for example, awk: cmd. line:1: ^ backslash not last character on line). Are you sure that you have provided the code you are really running? – roaima Dec 4 '15 at 13:28
  • 1
    What you see here is the state of the art before Perl. Perl was created to improve on this. – reinierpost Dec 4 '15 at 13:31

What you have lost is the trailing ) on your stdin subclause. What I suspect you have gained is a headache trying to read your code.

Try this instead, which then lends itself to further optimisation because it's (more) readable:

tr '[:blank:]' $'\t' <file2 |
    awk '
        BEGIN { FS="\t"; OFS="\t" }
        { if ($2 == "u") print $0, $1; else print $0, $3 }
    ' |
    awk '
        { gsub(/ /,"\t"); l=$4; sub(/.*_/,"",l); print $2 "\t" $3 "\t" l }
    ' |
    sort |
    join -t $'\t' -a1 -e "u" -1 1 -2 1 -o 1.1,2.1,2.2 file1 - >out
  • Thank you...this gives me the error "join: file 2 is not in sorted order"...I have file1 sorted right before...mmm I don't know anymore...it gave me a huge headache and a sleepless night to try to create this code so I am really looking forward to running it... – jamespower Dec 4 '15 at 14:53
  • @jamespower add another sort into the pipeline. I'll edit in a suggestion for you – roaima Dec 4 '15 at 15:12
  • I think it is because the commands this way are flipped... I am doing all of the operations after the merging not only to one file... – jamespower Dec 4 '15 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.