-2

I want to replace src='hello' with src='foobar' with sed. I believe it is with this command but it doesnt seem to work:

sed -i "s/(src=').*'/src='foobar'/g" index.php
jww
  • 97,681
  • 90
  • 411
  • 885
gusta888
  • 1
  • 2
  • 2
    `sed "s/src='hello'/src='foobar'/g" index.php` – Beta Jun 20 '18 at 23:53
  • I tried that but it doesnt seem to work and I want to try it on many iterations the replacing of text between the quotes of src=‘hello’ – gusta888 Jun 21 '18 at 00:55
  • 1
    What does "doesn't seem to work" mean in real, reproducible terms? Show us some input text, along with the results you were expecting and the results you got when you ran your command. Your "many iterations" are not something we can diagnose at this distance. We need just one, functional example. – ghoti Jun 21 '18 at 01:24
  • sed -i “s/(src=‘.*’)/src=‘foobar’/g” file1 – gusta888 Jun 21 '18 at 01:28
  • That input command did not save or make the change – gusta888 Jun 21 '18 at 01:29
  • src=‘hello’ did not change to src=‘foobar’ – gusta888 Jun 21 '18 at 01:48
  • [What characters do I need to escape when using sed in a sh script?](https://unix.stackexchange.com/q/32907/56041), [Escape a string for a sed replace pattern](https://stackoverflow.com/q/407523/608639), [How do I replace single quotes with another character in sed?](https://stackoverflow.com/q/17357952/608639), [How to escape single quote in sed?](https://stackoverflow.com/q/24509214/608639), etc. – jww Jun 21 '18 at 04:47
  • You are trying to solve too many problems at once, and you may be using an unusual version of `sed`. – Beta Jun 21 '18 at 15:07

1 Answers1

-1

Anyway, I figured it out. The following is the input command.

$ sed -i -E "s/(src=').*(')/src='foobar'/g" index.php

This would replace:

<pre>
  <img src='hello' />
  <img src='what' />
</pre>

With:

<pre>
  <img src='foobar' />
  <img src='foobar' />
</pre>
gusta888
  • 1
  • 2