将变量设置为awk函数的输出。

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

Setting variable to output of awk function

问题

我定义了一个名为dgrulawk函数,它返回一个字符串。

我有一个变量,我想将其设置为函数dgrul的输出。如何做到这一点?

以下是该函数:

function dgrul(categr,     rl)
{
  if ( ! categr ) { categr = "POSIX" }
  rl="[[:digit:]]"
  return rl
}

我想设置变量digt_rl的值,以便我可以将其用于构建其他模式。

英文:

I have defined an awk function dgrul that returns a string.

I have a variable which I want to set to the output of the function dgrul. How can I do this?

Here is the function

function dgrul(categr,     rl)
 {
  if ( ! categr ) { categr = "POSIX" }
  rl="[[:digit:]]"
  return rl
 }

I want to set the value digt_rl so that I can use it to construct other patterns.

答案1

得分: 1

同样的方式可以捕获awk字符串函数的结果,例如:

x = substr($1, 3, 5)

捕获用户自定义函数返回值的简单示例:

awk '
function dgrul(rl) {
    rl = "abcde"
    return rl
}
BEGIN {
    print "x (before) :", x
    x = dgrul()
    print "x (after) :", x
}
'

这将生成:

x (before) :
x (after) : abcde
英文:

Same way you'd capture an awk string function result, eg:

x = substr($1,3,5)

A simple example of capturing a user-defined function return value:

awk '
function dgrul( rl ) {
    rl = "abcde"
    return rl
}
BEGIN { print "x (before) :",x
        x = dgrul()
        print "x  (after) :",x
      }
'

This generates:

x (before) :
x  (after) : abcde

huangapple
  • 本文由 发表于 2023年3月4日 07:40:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632734.html
匿名

发表评论

匿名网友

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

确定