I'm trying to work up a sed command for the command below. I'm trying search for v=window.location.search.match in a file and replace it with <script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>

Here is what I have so far:

sed -i.bak 's/v=window.location.search.match/\<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"\>\</script\>/d' myfile.txt

After running this I get the following error:

sed: -e expression #1, char 49: unknown option to `s'

I'm confused as to where I should be placing each \ for escaping.

  • 2
    use this format: s_A_B_ ; this command replace all A with B – بارپابابا Nov 9 '16 at 20:37
  • Thank you, Babyy that seemed to have done it. I'm now just wondering what I do to replace the entire line that contained v=window.location.search.match instead of just that matches. – hashwagon Nov 9 '16 at 21:16

As pointed out by @Babyy, if your expression contains / characters, then you can change sed separator to something that won't risk to be found in your input file, such as | or _

sed -i.bak 's|v=window.location.search.match|<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>|' myfile.txt

Now if you want to replace the entire line, the first part of your expression should match everything, from line start to its end:

sed -i.bak 's|^.*v=window.location.search.match.*$|<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>|' myfile.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.