如何用 square(var) 查找和替换所有形式的 pow(var,2)? [英] How can I find and replace all forms of pow(var,2) with square(var)?

查看:41
本文介绍了如何用 square(var) 查找和替换所有形式的 pow(var,2)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到所有形式的 pow(var,2) 并用 square(var) 替换我当前目录的 C++ 文件中出现的所有形式.>

我正在浏览 https://regexr.com/,但我仍然不确定如何才能将 var 描述为正则表达式.复杂的是 var 是任何符合以下事实的变量名称的占位符:

  1. 它不包含空格
  2. pow(,2)
  3. 的约束
  4. 由大写字母[A-Z]、小写字母[a-z]组成,和/或下划线字符 _.在 Linux 中是否有一种规范的方法来进行这样的重构?

使用最少工作示例的更新 1:

输入:

pow(alpha,2) + pow(beta,2)(3*pow(betaR_red,2))2/pow(gammaBlue,3))-pow(epsilon_gamma,2)+5

期望的输出:

square(alpha) + square(beta)(3*square(betaR_red))2/pow(gammaBlue,3))-square(epsilon_gamma)+5

更新 2:

这是一个后续的链接 问题 执行此特定查找和替换任务有更多解决方案.

解决方案

条件和假设:

  • OP 提到需要处理多个文件;对于这个答案,我将专注于单个文件;如果多文件解决方案出现问题,OP 可以开始另一个问题
  • OP 提到想要 replace 一些字符串,但不清楚(对我来说)是覆盖原始文件还是创建新文件;对于这个答案,我将专注于生成修改后的"输出;OP 可以根据最终要求扩展此解决方案(如下)
  • 示例似乎暗示了 4 种不同的搜索模式(alphabetabetaR_redepsilon_gamma);我将假设需要搜索的模式数量可变
  • 为简单起见,我将假设搜索模式存储在数组中
  • 搜索模式不包含前导/尾随空格
  • 搜索模式相对简单,并且包含任何特殊字符(例如换行符)

示例输入文件:

$ cat input.txtpow(alpha,2) + pow(beta,2)(3*pow(betaR_red,2))2/pow(gammaBlue,3))-pow(epsilon_gamma,2)+5

搜索模式数组:

$ var=(alpha beta betaR_red epsilon_gamma '双螺旋')$排版 -p var声明 -a var=([0]=alpha"[1]=beta"[2]=betaR_red"[3]=epsilon_gamma"[4]=双螺旋")

总体思路是使用sed根据var[]数组的内容对文件进行多模式搜索.这意味着我们需要一种以适合 sed 多模式匹配的方式引用数组的方法(即,值需要用管道 (|>).

通过分配 IFS='|',我们可以重新格式化"数组内容以用作 sed 的多模式搜索字符串:

$ echo "${var[*]}";alpha beta betaR_red epsilon_gamma 双螺旋$IFS='|'varX="${var[*]}";;echo "${varX}";alpha|beta|betaR_red|epsilon_gamma|双螺旋

这将我们带到了 sed 命令:

$ IFS='|'sed -E "s/pow\((${var[*]}),2\)/square(\1)/g";输入文件

地点:

  • sed -E - 在扩展的正则表达式支持下运行
  • pow\(/,2\) - 搜索我们的 pow(..,2) 字符串,转义括号,以便它们不被评估为正则表达式组的分隔符
  • IFS='|'/(${var[*]}) - 使用 '|' 扩展数组 var 作为值分隔符;用括号括起来,这将成为我们的第一个(也是唯一一个)搜索组
  • square(/) - pow(/,2) 模式的替换字符串
  • \1 - 复制我们搜索组的内容,例如,如果我们匹配 pow(beta,2) 那么 \1 == 测试版

如果我们执行上面的 set -xv ;IFS='|'sed ...;set +xv 我们将生成以下调试"输出,显示如何使用 var 数组的值扩展 sed 命令:

++ IFS='|'++ sed -E 's/pow\((alpha|beta|betaR_red|epsilon_gamma|double helix),2\)/square(\1)/g' input.txt

上面sed命令的实际输出:

square(alpha) + square(beta) # 2x 变化(3*square(betaR_red)) # 1x 变化2/pow(gammaBlue,3)) # 没有变化-square(epsilon_gamma)+5 # 1x 变化

I would like to find and replace all forms of pow(var,2) occurring in the C++ files of my current directory with square(var).

I was looking through https://regexr.com/, but I am still not sure how I can describe var to regex. The complication is that var is a placeholder for any variable name which adheres to the following facts:

  1. It does not contain spaces
  2. It is bounded by pow( and ,2)
  3. It is composed of upper case letters [A-Z], lower case letters [a-z], and/or the underscore character _. Is there a canonical way to do such a refactoring in Linux?

Update 1 with Minimum Working Example:

Input:

pow(alpha,2) + pow(beta,2)
(3*pow(betaR_red,2))
2/pow(gammaBlue,3))
-pow(epsilon_gamma,2)+5

Desired Output:

square(alpha) + square(beta)
(3*square(betaR_red))
2/pow(gammaBlue,3))
-square(epsilon_gamma)+5

Update 2:

Here is a link to a follow-up question for which there are more solutions to performing this particular find and replace task.

解决方案

Provisos and assumptions:

  • OP mentions needing to process multiple files; for this answer I'm going to focus on a single file; OP can start another question if issues arise with a multi-file solution
  • OP mentions wanting to replace some strings but it's not clear (to me) if the original file is to be overwritten or a new file is to be created; for this answer I'm going to focus on generating the 'modified' output; OP can expand on this solution (below) based on final requirements
  • examples seem to imply 4x different search patterns (alpha, beta, betaR_red, epsilon_gamma); I'm going to assume there could be a variable number of patterns that need to be searched for
  • for simplicity sake I'm going to assume the search patterns are stored in an array
  • search patterns contain no leading/trailing white space
  • search patterns are relatively simple and do not contain any special characters (eg, line feeds)

Sample input file:

$ cat input.txt
pow(alpha,2) + pow(beta,2)
(3*pow(betaR_red,2))
2/pow(gammaBlue,3))
-pow(epsilon_gamma,2)+5

Array of search patterns:

$ var=(alpha beta betaR_red epsilon_gamma 'double helix')
$ typeset -p var
declare -a var=([0]="alpha" [1]="beta" [2]="betaR_red" [3]="epsilon_gamma" [4]="double helix")

The general idea is to use sed to do a multi-pattern search of the file based on the contents of the var[] array. This means we need a way to reference the array in a manner that will be suitable for a sed multi-pattern match (ie, values need to be separated by a pipe (|).

By assigning IFS='|' we can 'reformat' the array contents to work as a multi-pattern search string for sed:

$ echo "${var[*]}"
alpha beta betaR_red epsilon_gamma double helix
$ IFS='|' varX="${var[*]}" ; echo "${varX}"
alpha|beta|betaR_red|epsilon_gamma|double helix

Which brings us to the sed command:

$ IFS='|' sed -E "s/pow\((${var[*]}),2\)/square(\1)/g" input.txt

Where:

  • sed -E - run with extended regex support
  • pow\( / ,2\) - search for our pow(..,2) string, escaping the parens so they are not evaluated as delimiters of a regex group
  • IFS='|' / (${var[*]}) - expand array var using '|' as value delimiter; by wrapping in parens this becomes our first (and only) search group
  • square( / ) - replacement string for pow( / ,2) pattern
  • \1 - copy contents of our search group, eg, if we matched on pow(beta,2) then \1 == beta

If we execute the above as set -xv ; IFS='|' sed ...; set +xv we will generate the following 'debug' output showing how the sed command is expanded with the values of the var array:

++ IFS='|'
++ sed -E 's/pow\((alpha|beta|betaR_red|epsilon_gamma|double helix),2\)/square(\1)/g' input.txt

The actual output of the above sed command:

square(alpha) + square(beta)          # 2x changes
(3*square(betaR_red))                 # 1x change
2/pow(gammaBlue,3))                   # no changes
-square(epsilon_gamma)+5              # 1x change

这篇关于如何用 square(var) 查找和替换所有形式的 pow(var,2)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆