0

I have a file app.spec with the string:

Version:  1.0.0

I want to replace 1.0.0 with the variable version e.g. 1.0.1. The regex below which matches this but how can I replace only the first occurrence?

sed -i -E "s/[0-9]+\.[0-9]+\.[0-9]+/$version/" ./app.spec

I tried but it didn't work.

sed -i -E "0,/[0-9]+\.[0-9]+\.[0-9]+/$version/" ./app.spec
van
  • 8,079
  • 19
  • 57
  • 88
  • 2
    Does this answer your question? [How to use sed to replace only the first occurrence in a file?](https://stackoverflow.com/questions/148451/how-to-use-sed-to-replace-only-the-first-occurrence-in-a-file) – Daemon Painter Apr 27 '20 at 13:16
  • 1
    in the second case, you forgot the substitute command.. should be `sed -i -E '0,/[0-9]+\.[0-9]+\.[0-9]+/ s//'"$version"'/'` – Sundeep Apr 27 '20 at 13:25
  • @DaemonPainter Fair comment. – van Apr 27 '20 at 15:30
  • 1
    @Sundeep, Thank you! your answer helped me see I had missed the substitute as well as how to pass the variable when in single quotes. – van Apr 27 '20 at 15:30
  • 1
    glad I could help, see also https://stackoverflow.com/questions/7680504/sed-substitution-with-bash-variables for further details wrt using shell variables – Sundeep Apr 27 '20 at 15:39

0 Answers0