将匹配的值传递给函数,然后用返回值替换 [英] Pass the matched value to a function, and replace with the return value

查看:85
本文介绍了将匹配的值传递给函数,然后用返回值替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何使Bash匹配正则表达式,而不是将值替换为常量字符串,而是将匹配的值传递给函数,然后从的返回值中获取要替换为的值.功能.

How would I get Bash to match a regular expression, but rather than replace the value with a constant string, it will instead pass the matched value to a function, and then get the value to replace with from the return value of the function.

类似下面的伪代码,它用相同的字符但大写替换了[a-d]的每个匹配项:

Something like the following pseudocode, which replaces every match of [a-d] with the same character, but uppercase:

function uppercase() { echo ${1^^}; }
string="abcdefgh123cbazyz"
echo ${string//[a-d]/uppercase()}
# output: ABCDef123CBAzyz

我并不特别,可以使用通常安装在Unix系统上的任何语言(例如sedawk甚至是bash中内置的有限正则表达式支持).

I'm not particular, any language that is typically installed on a Unix system (such as sed, awk, or even the limited regex support built into bash) can be used.

推荐答案

Bash无法在要完成所需的操作,请使用模式匹配进行案例修改:

To accomplish what you want, use pattern matching with case modification:

string="abcdefgh123cbazyz"
echo ${string^^[a-d]}

输出:

ABCDefgh123CBAzyz

这篇关于将匹配的值传递给函数,然后用返回值替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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