0

I have a line of code which is like this:

grep -f <(awk '{ print $1 }' fileA.txt | sed 's/\(.*\),/\1 /' ) fileB.txt > fileC.txt

It is working when I run it on the terminal but when I add it inside a *.sh script it gives this error:

format_input.sh: line 12: syntax error near unexpected token `('

I tried to escape the () like this:

grep -f <\(awk '{ print $1 }' fileA.txt | sed 's/\(.*\),/\1 /' \) fileB.txt > fileC.txt

however, I still get some sort of error:

format_input.sh: line 12: (awk: No such file or directory
sed: ): No such file or directory

What am I doing wrong?

1
  • 1
    Have you ran it through shellcheck? – ctrl-alt-delor Jan 22 at 7:07
4

I am not sure if this is the correct answer, but I want to include more code than fits a comment. My guess is that you use a non-Bash shell to launch format_input.sh. I tested this with a simpler line. The file file contains this:

$ cat file
content
more content
this is a line
another one

Here is my test from the command line:

$ grep ON <(tr '[a-z]' '[A-Z]'< file)
CONTENT
MORE CONTENT
ANOTHER ONE

I put this line in a script that I name script and launch it like that:

$ bash script
CONTENT
MORE CONTENT
ANOTHER ONE

$ sh script
script: line 1: syntax error near unexpected token `('
script: line 1: `grep ON <(tr '[a-z]' '[A-Z]'< file)'

(This is on Cygwin, but I don't think the particular platform matters)

10
  • This looks like a good hypothesis. Insure that the first line of your script is #!/bin/bash – ctrl-alt-delor Jan 22 at 7:11
  • my first line is #!/bin/bash I included it – Paolo Lorenzini Jan 22 at 12:59
  • shellcheck says there is no error – Paolo Lorenzini Jan 22 at 13:05
  • 2
    @PaoloLorenzini make it executable (chmod +x format_input.sh) then execute it by path only (i.e. ./format_input.sh if it is located in the current directory); or place it somewhere in your PATH and then call it from anywhere as format_input.sh – steeldriver Jan 22 at 16:41
  • 1
    bash format_input.sh works, and so does steeldriver's suggestion. – berndbausch Jan 23 at 1:52

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.