1

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

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Lavanya
  • 11
  • 2
  • Thanks for showing your efforts in your question. But sorry this is not that clear, kindly do edit your sample of input and expected output in your question and let us know then. – RavinderSingh13 Aug 19 '20 at 09:30
  • Thank you for editing Lavanya, sorry its still not clear by which logic you want to achieve your shown expected output, kindly do add more details in your question and let us know then. – RavinderSingh13 Aug 19 '20 at 09:54
  • Using sed commands or any logic .. In output.txt file I should change the startTime to epoch timestamp(653790102 something like above) using ruby automation – Lavanya Aug 19 '20 at 10:01
  • Your problem is probably not sed but [bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash). – potong Aug 19 '20 at 10:29
  • could you please provide me the command – Lavanya Aug 19 '20 at 10:52

2 Answers2

3

Replace single quotes with double quotes

`sed -i -e "s/startTime/$time/g" ./db/output.txt`
paradox
  • 33
  • 3
1

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
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • while executing "bundle exec rake " command , I could see this error "sed: -e expression #1, char 26: unterminated `s' command" – Lavanya Aug 20 '20 at 04:15