简体   繁体   中英

sed command to replace string not working

Must be doing something silly here.

I have a file called 'test'. The file contains only one line -

versionName '2.0.0'

I want to programmatically replace the 2.0.0 value dynamically. Can't figure out why the following command not working -

sed -i '' 's/versionName  "[0-9.]*"/versionName "'${MAJOR_VERSION}'"/' test

here, MAJOR_VERSION is a variable.

Please let me know if I can provide more info.

Try

sed -i '' "s/versionName '[0-9.]*'/versionName '${MAJOR_VERSION}'/" test

There were a couple of issues :

  • Your line had single-quotes, but your sed command was trying to match on double-quotes
  • Your line had a single space, but your sed command had two-spaces

eg:

 $ export MAJOR_VERSION=xyz
 % echo "versionName '2.0.0'" > test
 $ sed -i '' "s/versionName '[0-9.]*'/versionName '${MAJOR_VERSION}'/" test
 $ cat test

results in :

versionName 'xyz'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM