简体   繁体   中英

Sed regex to find-replace version numbers

I'm new to sed, trying to write a script to find/replace text in a file. The file (test.txt) looks like this;

   hello_world (1.2.0.123)

and I'm finding that this script (which I inherited):

sed -i 's/\(^\s*hello_world \)(.*)/\1hello_world (1.2.0.456)/' test.txt

is leading to;

   hello_world hello_world (1.2.0.456)

when I need it to be

   hello_world (1.2.0.456)

I'm not sure how to make the first part match only the parentheses, any assistance would be appreciated.

EDIT

  • The whitespace before the hello_world is important
  • The sed line is being auto-generated using variables etc. I'm looking for a way to make this regex work without changing that. The variables I have to play with are

    • variable1: hello_world
    • variable2: hello_world (1.2.0.456)

(hopefully it's obvious where these variables sat within the sed expression)

EDIT

I got this sorted in the end, answer below if anyone else is interested.

得到它了

 sed -i 's/\(^\s*\)phoenix_utils (.*)/\1phoenix_utils (1.0.0.28583)/' test.txt
sed -i -e 's/^\([[:blank:]]*hello_world \).*/\1(1.0.0.28583)/' YourFile

\\1 is the content of first ( ) so \\1Helloworld write it twice in your sample

be carefull with escape content depending of -e or not (behavior change and non GNU sed often need to escape ( for grouping pattern)

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