pass dynamic value in the sed command in unix inside IT block ruby - 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

Related

Sed insert blank line below search string then insert string on the next line

I have a sed expression in a function that I pass parameters to.
insert_after_new_line() {
if ! grep -q "${2}" "${3}"; then
sed -i "/${1}/{N;N;a ${2}}" "${3}"
fi
}
insert_after_new_line search insert textfile.txt
I am trying to have a blank line inserted below the search string and the insert string inserted after.
so
text
text
search
text
becomes
text
text
search
insert
text
but I keep getting the error
sed: -e expression #1, char 0: unmatched `{'
I tested this. works in command line
sed -i '/search/a \\ninsert' file
sed really delimiters commands by a newline. There is a ; but it does not work for all commands, mostly these which take file name as an argument. ; does not work for r R or for example a. Sed will read everything after a command, so sed interprets is like a ${2}} as a single command, in result it does not find enclosing }, cause it's been eaten by a command. You need a newline:
sed -i "/${1}/{N;N;a ${2}
}" "${3}"
or
sed -i "/${1}/{N;N;a ${2}"$'\n'"}" "${3}"
this should work:
sed -i '/search/{G;ainsert
}' file
You can replace the text by shell variable, but replace the single quotes by double quotes too.

Why does sed replace my quote marks?

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

Sed an entire line with only the begin of the line

I have a config file with this line:
foo.bar = http://localhost/foo
I want to replace this line with a new url. For example:
foo.bar = http://new_url/bar
But I don't know the original url, I only know the name of the parameter.
I tried this:
sed "s!^foo.bar.*!foo.bar = http://new_url/bar!" test.ini
But it return an error
Use single quotes to prevent shell expansion:
sed -e 's!^foo.bar.*!foo.bar = http://new_url/bar!'
In bash double-quotes and ! don't go well when placed inside ( see HISTORY EXPANSION in man bash), change the separator to # (or) use single-quotes
sed 's#foo.bar.*#foo.bar = http://new_url/bar#' file
foo.bar = http://new_url/bar

How do I replace text using a variable in a shell script

I have a variable with a bunch of data.
text = "ABCDEFGHIJK"
file = garbage.txt //iiuhdsfiuhdsihf]sdiuhdfoidsoijsf
What I would like to do is replace the ] charachter in file with text. I've tried using sed but I keep getting odd errors.
output should be:
//iiuhdsfiuhdsihfABCDEFGHIJKsdiuhdfoidsoijsf
Just need to escape the ] character with a \ in regex:
text="ABCDEFGHIJK"
sed "s/\(.*\)\]\(.*\)/\1$text\2/" file > file.changed
or, for in-place editing:
sed -i "s/\(.*\)\]\(.*\)/\1$text\2/" file
Test:
sed "s/\(.*\)\]\(.*\)/\1$text\2/" <<< "iiuhdsfiuhdsihf]sdiuhdfoidsoijsf"
# output => iiuhdsfiuhdsihfABCDEFGHIJKsdiuhdfoidsoijsf
There is always the bash way that should work in your osx:
filevar=$(cat file)
echo "${filevar/]/$text}" #to replace first occurence
OR
echo "${filevar//]/$text}" #to replace all occurences
In my bash i don't even have to escape ].
By the way, the simple sed does not work?
$ a="AA"
$ echo "garbage.txt //iiuhdsfiuhdsihf]sdiuhdfoidsoijsf" |sed "s/]/$a/g"
garbage.txt //iiuhdsfiuhdsihfAAsdiuhdfoidsoijsf

sed with replacement part given as input from command line

I am trying to pass command line argument as the replacement part to sed command. But $1 doesn't work for me.
For example I unsuccesfully tried
function changeversion() {
sed -i 's/[0-9]\+/$1/g' file.xml;
}
which only replaced all numbers in file with '$1' text.
How can I fix it?
I think you need to put the expression in double qoutes to get variable substitution
function changeversion() {
sed -i "s/[0-9]\+/$1/g" file.xml;
}

Resources