动态sed替换字符串的一部分

huangapple go评论56阅读模式
英文:

Dynamic sed to replace part of string

问题

以下是您要翻译的部分:

Well I have a project that was made upgrade from php 7.4 to 8.1

so many functions in someway doesn't work as before because in php 8.1 doesn't accept null anymore.

So I want to replace all functions with a script so I could automation it.

Example:
     line not affected before

explode(self::RULE_KEY_SEPARATOR, $key); somethingAfter
testeBefore explode(somthing, $key); somethingAfter
line not affected after

Output:
     line not affected before

explode(self::RULE_KEY_SEPARATOR, $key ?? '''); somethingAfter
testeBefore explode(something, $key ?? '''); somethingAfter
line not affected after

So a solution could be add '?? ''' before close the parentesis of the function

here a snippet of what I tried to do: https://sed.js.org/?snippet=xqo9gd

I don't know if this is possible with sed. 

If someone could help. I would appreciate :)

**EDIT:**

if snippet isn't available I will let here my try and output

I tried:

's/^(.??).explode(.)./explode(self::RULE_KEY_SEPARATOR, $key ?? ''''''');/g'

Output:
     line not affected before

explode(self::RULE_KEY_SEPARATOR, $key ?? ''');
explode(self::RULE_KEY_SEPARATOR, $key ?? ''');
line not affected after

As expected since is not dynamically...
英文:

Well I have a project that was made upgrade from php 7.4 to 8.1

so many functions in someway doesn't work as before because in php 8.1 doesn't accept null anymore.

So I want to replace all functions with a script so I could automation it.

Example:

         line not affected before
explode(self::RULE_KEY_SEPARATOR, $key); somethingAfter
         testeBefore  explode(somthing, $key); somethingAfter
line not affected after

Output:

         line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? ''); somethingAfter
         testeBefore  explode(something, $key ?? ''); somethingAfter
line not affected after

So a solution could be add ?? '' before close the parentesis of the function

here a snippet of what I tried to do: https://sed.js.org/?snippet=xqo9gd

I don't know if this is possible with sed.

If someone could help. I would appreciate 动态sed替换字符串的一部分

EDIT:

if snippet isn't available I will let here my try and output

I tried:

's/^\(.*\?\?\).*explode\(.*\).*/explode(self::RULE_KEY_SEPARATOR, $key ?? '\'''\''\);/g'

Output:

         line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? '');
explode(self::RULE_KEY_SEPARATOR, $key ?? '');
line not affected after

As expected since is not dynamically...

答案1

得分: 1

以下是翻译好的部分:

  • 你创建了一个捕获组,但没有使用它,因此失去了所有前导空白。
  • explode之后,你又创建了另一个捕获组,而不是字面匹配括号。
  • 在第二个捕获组之后,你使用了贪婪正则表达式来捕获并丢弃行的其余部分。
  • 你是在硬编码替换值,而不是使用已经存在的值。

你可以尝试使用这个sed命令:

$ sed "s/explode([^)]*/& ?? ''/" input_file
         line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? ''); somethingAfter
         testeBefore  explode(somthing, $key ?? ''); somethingAfter
line not affected after
英文:

Your current command has a few issues;

  • You create a capture group but do not utilize it therefore losing all leading whitespace
  • After explode, you are creating another capture group rather than matching the parenthesis literally
  • After the second capture group, you use a greedy regex to capture and discard the remainder of the line.
  • You are hardcoding the replace value rather than using the values already present

You can try this sed

$ sed "s/explode([^)]*/& ?? ''/" input_file
         line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? ''); somethingAfter
         testeBefore  explode(somthing, $key ?? ''); somethingAfter
line not affected after

huangapple
  • 本文由 发表于 2023年3月9日 23:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定