0

I have been bashing my head against the wall trying to get sed to search a file are replace a timestamp.

example replace strings (file):

<script src="<@ofbizContentUrl>/xxx/js/v6/jsfile.js?v=201711271300</@ofbizContentUrl>"></script>

<script src="<@ofbizContentUrl>/xxx/js/v6/jsfile2.js?v=20171127245</@ofbizContentUrl>"></script>

basically I want to replace the 201711271300 in the examples with the current timestamp which i have saved in a variable date. The dates in the example differ from case to case.

I've been trying

sudo sed -i "s/?v=.+?(?=<)/?v=${date}/g" file

to no avail, going on 2 hours now

4
  • You can do this with less headache with python in couple of lines. Do you have to use ancient sed? Commented Nov 28, 2017 at 19:30
  • 2
    sed doesn't support lookahead regex Commented Nov 28, 2017 at 19:31
  • i guess i dont basically we have a shell script that deploys our nee builds of our website so i was trying to do it on the fly during the build process but i guess i can just add a python command in the sh script that will handle the replacement Commented Nov 28, 2017 at 19:32
  • @anubhava uhg thanks Commented Nov 28, 2017 at 19:32

1 Answer 1

2

Try this one

sed "s/?v=\([0-9]*<\)/?v=${date}</g" $file

It uses a simpler pattern: all digits until you hit a <. Replace by a data (and restore the <).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.