替换为sed + bash功能 [英] Substitution with sed + bash function

查看:98
本文介绍了替换为sed + bash功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题似乎很笼统,但是我找不到任何答案.

my question seems to be general, but i can't find any answers.

在sed命令中,如何用简单的bash函数返回的值替换替换模式.

In sed command, how can you replace the substitution pattern by a value returned by a simple bash function.

例如,我创建了以下函数:

For instance, I created the following function :

function parseDates(){
    #Some process here with $1 (the pattern found)
    return "dateParsed;
}

和以下sed命令:

myCatFile=`sed -e "s/[0-3][0-9]\/[0-1][0-9]\/[0-9][0-9]/& parseDates &\}/p" myfile`

我发现角色&"表示找到的当前模式,我希望将其传递给我的bash函数,并将整个模式替换为+ dateParsed找到的模式.

I found that the caracter '&' represents the current pattern found, i'd like it to be passed to my bash function and the whole pattern to be substituted by the pattern found +dateParsed.

有人有想法吗? 谢谢

推荐答案

您可以通过结束用单引号引起的部分并将其重新打开来将sed​​命令粘合在一起.

You can glue together a sed-command by ending a single-quoted section, and reopening it again.

sed -n 's|[0-3][0-9]/[0-1][0-9]/[0-9][0-9]|& '$(parseDates)' &|p' datefile

但是,与其他示例相比,bash中的函数无法返回字符串,只能将其取出:

However, in contrast to other examples, a function in bash can't return strings, only put them out:

function parseDates(){
    # Some process here with $1 (the pattern found)
    echo dateParsed
}

这篇关于替换为sed + bash功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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