Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed error near unexpected token '(' where ( is part of search string

Tags:

regex

bash

sed

I try to search and replace a given string in a Makefile, that is

PROCFLAGS="SQLCHECK=SEMANTICS USERID=$(CONNECT)"

Where I want to add CODE=ANSI_C to the line

PROCFLAGS="SQLCHECK=SEMANTICS USERID=$(CONNECT) CODE=ANSI_C"

Sed should do this trick very good.. at least that's what I thought but I can't get behind what syntax error I am doing calling sed:

sed -i'.bak' s:(CONNECT):(CONNECT) CODE=ANSI_C:g Makefile

I have tried to add -r or escape the brackets \( \) but I always end up with the error:

-bash: syntax error near unexpected token `('

I can only think of something I must be doing wrong here as many other replacements really work fine.

like image 931
Stefan Avatar asked Oct 24 '25 02:10

Stefan


2 Answers

Try quoting the bracketed items. Bash is attempting to run (CONNECT) in a sub shell I think

sed -i'.bak' "s:(CONNECT):(CONNECT) CODE=ANSI_C:g" Makefile

When you put something in brackets, bash will run it as a command in a subshell.

For example, try (cd /tmp; pwd); pwd. You will see the subshell print /tmp, but then the second pwd will tell you the original directory.

like image 186
Squidly Avatar answered Oct 25 '25 16:10

Squidly


Put your sed code inside quotes.

sed -i'.bak' 's:(CONNECT):(CONNECT) CODE=ANSI_C:g' Makefile
like image 34
Avinash Raj Avatar answered Oct 25 '25 15:10

Avinash Raj