如何检查awk变量的类型? [英] How to check the type of an awk variable?

查看:117
本文介绍了如何检查awk变量的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gawk 4.2.0的Beta版本,可在 http:中获得//www.skeeve.com/gawk/gawk-4.1.65.tar.gz 是主要版本,具有许多重要的新功能.

The Beta release of gawk 4.2.0, available in http://www.skeeve.com/gawk/gawk-4.1.65.tar.gz is a major release, with many significant new features.

我之前曾问过 FS的行为是什么?"在GNU Awk 4.2中?,现在我注意到全新的 typeof()函数弃用了 isarray() :

I previously asked about What is the behaviour of FS = " " in GNU Awk 4.2?, and now I noticed the brand new typeof() function to deprecate isarray():

从4.1.4更改为4.2.0

Changes from 4.1.4 to 4.2.0

  1. 新的 typeof()函数可用于指示变量或数组元素是数组,正则表达式,字符串还是数字.不推荐使用 isarray()函数,而推荐使用 typeof().
  1. The new typeof() function can be used to indicate if a variable or array element is an array, regexp, string or number. The isarray() function is deprecated in favor of typeof().

我可以介绍四种情况:字符串,数字,数组和未分配:

I could cover four cases: string, number, array and unassigned:

$ awk 'BEGIN {print typeof("a")}'
string
$ awk 'BEGIN {print typeof(1)}'
number
$ awk 'BEGIN {print typeof(a[1])}'
unassigned
$ awk 'BEGIN {a[1]=1; print typeof(a)}'
array

但是,我很难获得"regexp"因为我的尝试都没有达到这个目标,并且总是产生数字":

However, I struggle to get "regexp" since none of my attempts reach that and always yield "number":

$ awk 'BEGIN {print typeof(/a/)}'
number
$ awk 'BEGIN {print typeof(/a*/)}'
number
$ awk 'BEGIN {print typeof(/a*d/)}'
number
$ awk 'BEGIN {print typeof(!/a*d/)}'
number
$ awk -v var="/a/" 'BEGIN{print typeof(var)}'
string
$ awk -v var=/a/ 'BEGIN{print typeof(var)}'
string

如何获取定义为"regexp"的变量?

How can I get a variable to be defined as "regexp"?

我注意到上一个项目符号:

I noticed the previous bullet:

  1. Gawk现在支持强类型的正则表达式常量.这样的常量看起来像@/.../.您可以将它们分配给变量,将它们传递给函数,在〜,!〜和switch语句的case部分中使用它们.手册中提供了更多详细信息.

尝试了一下,但是没有运气:

And tried a bit, but with no luck:

$ awk -v pat=@/a/ '{print typeof(pat)}' <<< "bla ble"
string

推荐答案

typeof(/a/) $ 0的结果上运行 typeof()〜/a/是一个数字.我本人还没有尝试过,但是我希望这是您正在寻找的东西:

typeof(/a/) is running typeof() on the result of $0 ~ /a/ which is a number. I haven't tried this yet myself but I'd expect this to be what you're looking for:

typeof(@/a/)

var = @/a/
typeof(var)


这可行:


So this works:

$ awk 'BEGIN {print typeof(@/a/)}'
regexp

$ awk 'BEGIN {var=@/a/; print typeof(var)}'
regexp

这篇关于如何检查awk变量的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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