Why does sed replace my quote marks? - bash

I have a SQL query which contains:
join (&max_dt1) t3
I want to replace &max_dt1 so:
MAX_DT1="'2017-11-03'"
REPLACE="sed -i 's/&max_dt1/select ($MAX_DT1) as dt/g' $FINAL_QUERY"
cat $FINAL_QUERY | eval $REPLACE
Result:
join (select (2017-05-09) as dt) t3
Why is it deleting the quote marks of my variable MAX_DT1?
My real code:
MAX_DT1="'$MAX_DT1'"
echo $MAX_DT1
REPLACE="sed -i 's/&max_dt1/select ($MAX_DT1) as dt/g' $FINAL_QUERY"
echo sed -i "s/&max_dt1/select ($MAX_DT1) as dt/g $FINAL_QUERY
cat $FINAL_QUERY | eval $REPLACE
MAX_DT1 first of all contains just: 2017-11-03

follow this
$ query="join (&max_dt1) t3"
$ MAX_DT1="'2017-11-03'" # <--- you have to quote the single quotes!!
$ sed "s/&max_dt1/select ($MAX_DT1) as dt/g" <<< $query
join (select ('2017-11-03') as dt) t3

Quotes indicates that you affect a string to MAX_DT1 variable. It's not part of your string.
If your variable value has to contains quotes then you have to add them.
For instance:
MAX_DT1="'2017-11-03'"
Unfortunately your Next REPLACE variable won't have what you're expecting and the eval command called later won't work since its sed command would be bad formatted (if you keep your code this way).
Fortunately, your problem is solved if you call directly the sed command this way:
MAX_DT1="'2017-11-03'"
sed -i "s/&max_dt1/select ($MAX_DT1) as dt/g" $FINAL_QUERY

Related

Bash HEREDOC Single Quotes around expanded variable

I need the $table var to expand but while keeping the quotes that are required by MSSQL for the table_name parameter. I don't know if this is possible as I have been searching for a while. The common answer I see is if there are any quotes then variables won't be expanded. Is it simply not possible to do what I need here?
Code
cat <<EOF | isql $host sa 'password' -d, | sed '-e 1,10d;$d' | sort > mssql_table_${table}_column_info
use $database;
select column_name from information_schema.columns where table_name = '$table';
EOF
Desired Output
select column_name from information_schema.columns where table_name = 'mytable_name';
Notice that the output has single quotes still around the table name. This is necessary for MSSQL to select the appropriate table.
Are you sure the variable expansion in the here document is the problem though? If you just inspect the output of the cat command (using bash):
$ database=database123 table=mytable_name cat <<EOF >/dev/stdout
use $database;
select column_name from information_schema.columns where table_name = '$table';
EOF
use database123;
select column_name from information_schema.columns where table_name = 'mytable_name';
You might want to break down what the other commands are doing to make sure where the error is.
On a side note, your mention of turning off the variable expansion via quotation marks (' or ") apparently conflates the syntax of the here document with the unrelated syntax of other commands in the pipeline.
For example this is correct:
### works, prints 'hello hello'
MYVAR='hello' cat <<EOF | grep 'hello hello'
hello $MYVAR
EOF
As opposed to:
### WRONG, response empty
MYVAR='hello' cat <<'EOF' | grep 'hello hello'
hello $MYVAR
EOF
Does not perform the variable substitution, because the word 'EOF' is quoted in the here document, turning off variable expansion. This is regardless of whether other commands in the pipeline, that is grep 'hello hello', have quotes or not.

pass dynamic value in the sed command in unix inside IT block ruby

output.txt:
select * from table startTime.... (very big query)
abc.rb:
it "example" do
time = 6537290102
replacestarttime = `sed -i -e 's/startTime/$time/g' ./db/output.txt`
end
expected output----
output.txt:
select * from table 6537290102....
In output.txt, the value should be 6537290102 please someone help. thanks in advance
If I hardcode the time value as shown below in sed command then it is working fine whereas the passing the dynamic value "time" is not working
replacestarttime = sed -i -e 's/startTime/6537290102/g' ./db/output.txt
Replace single quotes with double quotes
`sed -i -e "s/startTime/$time/g" ./db/output.txt`
Interpolate Your String in Ruby
Right now, you're expecting sed to access a shell variable named $time. This variable doesn't exist, and wouldn't work as-is even if it did.
To fix this, you should to interpolate your Ruby variable into the string passed to sed. You can do this as follows:
it "example" do
time = 6537290102
replacestarttime = `sed -i -e 's/startTime/#{time}/g' ./db/output.txt`
end

find text in file and then when found take the word on same line and insert into code

i want to insert the first word from file.txt when it matches string in my documents, i have like 500 documents like this so it would be nice if it works
file.txt looks like this:
test1 t1
test2 t2
test3 t3
this is my code
code="t1"
sed -i -e 's/^/Name="$code" /'
this will result in
code="t1"
sed -i -e 's/^/Name="t1" /'
this what i want for final output in all my documents:
document1.txt
code="t1"
sed -i -e 's/^/Name="test1" /'
document2.txt
code="t2"
sed -i -e 's/^/Name="test2" /'
I don't clearly understand your requirement. If you cant to extract the first word depending on the second word and then do some string manipulation, following might help.
file.txt content:
test1 t1
test2 t2
test3 t3
Extract first word, given the second word:
second="t1" # change this for a different match
first=$(sed -n "s/\(.*\) $second/\1/p" file.txt) # now $first is "test1"
echo "first word is ${first} and blah..." # write it to a file or do anything you need here

Concatenation String in shell

I have this shell line to concatenate 2 string:
new_group="second result is: ${result},\"${policyActivite}_${policyApplication}\""
echo "result is: ${new_group}"
The result:
result is: "Team","Application_Team"
How can change the result to: result is: "Team, Application_Team"
Use sed:
echo "$new_group" | sed 's/"//g;s/^\(.*\)$/"\1"/'
The first statement is removing all double quotes. The second one add double at the start and the end of the line.
Alternatively, if you want to replace "," with ,, use this sed command: sed 's/","/, /g'

Substitute variable in sed inside Makefile

I've been throught all possible answers in SO but still I cannot make my script work.
I have a query script in SPARQL with a line that needs to substitute the mark %contributor% by a variable between "<>"
?link dcterms:contributor %contributor%
Into
?link dcterms:contributor <http://newurl>
If I execute the code in the shell, the substitution is done properly and I see my query with the %contributor% tag successfully substituted. But when executed inside a Makefile, even with the double $$ dollar to allow the expansion of variables I don't manage to get it done.
for l in `cat sref.csv`; do \
QUERY=$$(cat queries/table_knowledge.rq | sed "s#%contributor%#<$$l>#g") ; \
echo $$QUERY ; \
done
Also like this it does not work:
QUERY=$$(cat queries/table_knowledge.rq | sed 's/%contributor%/<$(l)>/g') ; \
or
QUERY=$$(cat queries/table_knowledge.rq | sed 's/%contributor%/<${l}>/g') ; \
Use double quotes so that sed would expand variables.
Use a separator different than / since the replacement contains /
QUERY=$(cat queries/table_knowledge.rq | sed "s|%contributor%|<${l}>|g") ;
Test:
$ test="http://test.com"
$ echo "?link dcterms:contributor %contributor%" > source.file
$ x=$(cat source.file|sed "s|%contributor%|<${test}>|g")
$ echo $x
?link dcterms:contributor <http://test.com>

Resources