替换为sed + bash功能 [英] Substitution with sed + bash function
问题描述
我的问题似乎很笼统,但是我找不到任何答案.
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屋!