将变量传递给 awk 并在正则表达式中使用它

发布于 2024-08-21 03:57:49 字数 2131 浏览 1 评论 0原文

我正在学习 awk,但在将变量传递给脚本并将其用作正则表达式搜索模式的一部分时遇到问题。

这个例子是人为的,但显示了我的问题。

我的数据如下:

Eddy        Smith       0600000000  1981-07-16    Los Angeles
Frank       Smith       0611111111  1947-04-29    Chicago           
Victoria    McSmith     0687654321  1982-12-16    Los Angeles
Barbara     Smithy      0633244321  1984-06-24    Boston            
Jane        McSmithy    0612345678  1947-01-15    Chicago               
Grace       Jones       0622222222  1985-10-07    Los Angeles
Bernard     Jones       0647658763  1988-01-01    New York          
George      Jonesy      0623428948  1983-01-01    New York          
Indiana     McJones     0698732298  1952-01-01    Miami             
Philip      McJonesy    0644238523  1954-01-01    Miami

我想要一个 awk 脚本,我可以传递一个变量,然后让 awk 脚本为该变量执行正则表达式。 我现在有了这个名为“003_search_persons.awk”的脚本。

#this awk script looks for a certain name, returns firstName, lastName and City

#print column headers
BEGIN {
    printf "firstName lastName City\n";
}

#look for the name, print firstName, lastName and City
$2 ~ name {
    printf $1 " " $2 " " $5 " " $6;
    printf "\n";
}

我这样称呼该脚本:

awk -f 003_search_persons.awk name=Smith 003_persons.txt

它返回以下内容,这很好。

firstName lastName City
Eddy Smith Los Angeles
Frank Smith Chicago
Victoria McSmith Los Angeles
Barbara Smithy Boston
Jane McSmithy Chicago

但现在我想寻找某个前缀“Mc”。我当然可以对此进行硬编码,但我想要一个灵活的 awk 脚本。我在 003_search_persons_prefix.awk 中编写了以下内容。

#this awk script looks for a certain prefix to a name, returns firstName, lastName and City

#print column headers
BEGIN {
    printf "firstName lastName City\n";
}

#look for the prefix, print firstName, lastName and City
/^prefix/{
    printf $1 " " $2 " " $5 " " $6;
    printf "\n";
}

我这样调用脚本:

awk -f 003_search_persons_prefix.awk prefix=Mc 003_persons.txt

但现在它找不到任何记录。

问题在于搜索模式“/^prefix/”。我知道我可以用非正则表达式替换该搜索模式,如第一个脚本中所示,但假设我想使用正则表达式来执行此操作,因为我需要前缀真正位于姓氏字段的开头,因为它应该是,作为前缀和所有;-)

我该怎么做?

I'm learning awk and I have trouble passing a variable to the script AND using it as part of a regex search pattern.

The example is contrived but shows my probem.

My data is the following:

Eddy        Smith       0600000000  1981-07-16    Los Angeles
Frank       Smith       0611111111  1947-04-29    Chicago           
Victoria    McSmith     0687654321  1982-12-16    Los Angeles
Barbara     Smithy      0633244321  1984-06-24    Boston            
Jane        McSmithy    0612345678  1947-01-15    Chicago               
Grace       Jones       0622222222  1985-10-07    Los Angeles
Bernard     Jones       0647658763  1988-01-01    New York          
George      Jonesy      0623428948  1983-01-01    New York          
Indiana     McJones     0698732298  1952-01-01    Miami             
Philip      McJonesy    0644238523  1954-01-01    Miami

I want an awk script that I can pass a variable and then have the awk script do a regex for the variable.
I've got this script now called "003_search_persons.awk".

#this awk script looks for a certain name, returns firstName, lastName and City

#print column headers
BEGIN {
    printf "firstName lastName City\n";
}

#look for the name, print firstName, lastName and City
$2 ~ name {
    printf $1 " " $2 " " $5 " " $6;
    printf "\n";
}

I call the script like this:

awk -f 003_search_persons.awk name=Smith 003_persons.txt

It returns the following, which is good.

firstName lastName City
Eddy Smith Los Angeles
Frank Smith Chicago
Victoria McSmith Los Angeles
Barbara Smithy Boston
Jane McSmithy Chicago

But now I want to look for a certain prefix "Mc". I could ofcourse hardcode this, but I want an awk script that is flexible. I wrote the following in 003_search_persons_prefix.awk.

#this awk script looks for a certain prefix to a name, returns firstName, lastName and City

#print column headers
BEGIN {
    printf "firstName lastName City\n";
}

#look for the prefix, print firstName, lastName and City
/^prefix/{
    printf $1 " " $2 " " $5 " " $6;
    printf "\n";
}

I call the script like this:

awk -f 003_search_persons_prefix.awk prefix=Mc 003_persons.txt

But now it finds no records.

The problem is the search pattern "/^prefix/". I know I can replace that search pattern by a non-regex one, as in the first script, but suppose I want to do it with a regex, because I need the prefix to really be at the start of the lastName field, as it should be, being a prefix and all ;-)

How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落花随流水 2024-08-28 03:57:49

你可以尝试这个

BEGIN{
 printf "firstName lastName City\n";
 split(ARGV[1], n,"=")
 prefix=n[2]
 pat="^"prefix
}
$0 ~ pat{
    print "found: "$0
}

输出

$ awk -f  test.awk name=Jane file
firstName lastName City
found: Jane        McSmithy    0612345678  1947-01-15    Chicago

查看awk文档更多的。 (并从头到尾阅读它!)

you can try this

BEGIN{
 printf "firstName lastName City\n";
 split(ARGV[1], n,"=")
 prefix=n[2]
 pat="^"prefix
}
$0 ~ pat{
    print "found: "$0
}

output

$ awk -f  test.awk name=Jane file
firstName lastName City
found: Jane        McSmithy    0612345678  1947-01-15    Chicago

Look at the awk documentation for more. (and read it from start to finish!)

温暖的光 2024-08-28 03:57:49

将您的脚本更改为:

BEGIN {
    print "firstName", "lastName", "City"
    ORS = "\n\n"
}

$0 ~ "^" prefix {
    print $1, $2, $5, $6
}

并将其命名为

awk -v prefix="Mc" -f 003_search_persons.awk 003_persons.txt

Change your script to:

BEGIN {
    print "firstName", "lastName", "City"
    ORS = "\n\n"
}

$0 ~ "^" prefix {
    print $1, $2, $5, $6
}

and call it as

awk -v prefix="Mc" -f 003_search_persons.awk 003_persons.txt
So尛奶瓶 2024-08-28 03:57:49

您应该能够不加更改地使用原始脚本 - $2 ~ name 已经在进行正则表达式搜索,因此如果您使用 name=^Mc 调用脚本,它将返回名称以“麦”开头。实际上这不是一个很好的例子,因为 Mc 只出现在名称的开头 - 如果您使用 name=^Smith 那么它将找到 Smiths 而不是 McSmiths。

You should be able to use your original script unchanged - $2 ~ name is already doing a regex search so if you call your script with name=^Mc then it will return names starting with "Mc". Actually this is not a good example, since Mc only appears at the start of the name - if you use name=^Smith then it will find the Smiths but not the McSmiths.

梅窗月明清似水 2024-08-28 03:57:49

awk 是特别需要的吗?我确信这在 awk 中很有可能,但我不知道,如果你只是需要完成工作那么你可以尝试。但不确定该分隔符到底是什么。

cut -d " " -f1-2,5 file | egrep '^regex'

is awk specifically required? I'm sure it's quite possible in awk, but i don't know it, if you just need to get the job done then you can try. not sure exactly what that delimiter is though.

cut -d " " -f1-2,5 file | egrep '^regex'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文