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? Commented Jan 22, 2021 at 7:07

1 Answer 1

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 Commented Jan 22, 2021 at 7:11
  • my first line is #!/bin/bash I included it Commented Jan 22, 2021 at 12:59
  • shellcheck says there is no error Commented Jan 22, 2021 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 Commented Jan 22, 2021 at 16:41
  • 1
    bash format_input.sh works, and so does steeldriver's suggestion. Commented Jan 23, 2021 at 1:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.