R_S_C
R_S_C

Reputation: 75

String not getting replaced with sed command

In my script, I am using below sed command

while read line; do
  echo "$line"
  sed -r "s/where /where $line ;/" Query.cql > Query-2.cql
done < $1

the value of $line is

where Column11 in ('Value1','Value2','Vlaue3') and Column12 in ('Value11','Value22','Vlaue32')

File Content ::

capture 'data.csv'

select * from test where

capture off;

After Executing

sed -r "s/where /where $line ;/" Query.cql > Query-2.cql

OUTPUT is ::

capture 'data.csv'

select * from test where

capture off;

Here, the string is not getting replaced. What went wrong?

Upvotes: 0

Views: 425

Answers (1)

KamilCuk
KamilCuk

Reputation: 141890

sed: -e expression #1, char 19: unterminated `s' command

Sure the s command is s/<something>/<else>/ - there is a trailing /. Do:

sed -r 's/where /$line ;/'
                        ^ - trailing /

The -r option seems unused - where has no extended regular expressions. If so, remove it.

Your command uses ' quotes, so $line is not expanded. Research the difference between single and double quotes in shell, most probably you meant to use " here.

Note that each loop > qWithWhere.cql is recreating result and overwriting the result from previous loop. You might just run the loop on the last line them.

Read how to read a file line by line in bash and how to Escape a string for a sed replace pattern .


The following code with a space after where in input:

cat <<EOF >input
capture 'data.csv'
select * from test where 
capture off;
EOF
line="where Column11 in ('Value1','Value2','Vlaue3') and Column12 in ('Value11','Value22','Vlaue32')"
sed -r "s/where /where $line ;/" input

outputs:

capture 'data.csv'
select * from test where where Column11 in ('Value1','Value2','Vlaue3') and Column12 in ('Value11','Value22','Vlaue32') ;
capture off;
                  

Upvotes: 1

Related Questions