将变量传递给 awk 并在正则表达式中使用它
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以尝试这个
输出
查看awk文档更多的。 (并从头到尾阅读它!)
you can try this
output
Look at the awk documentation for more. (and read it from start to finish!)
将您的脚本更改为:
并将其命名为
Change your script to:
and call it as
您应该能够不加更改地使用原始脚本 -
$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 withname=^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 usename=^Smith
then it will find the Smiths but not the McSmiths.awk 是特别需要的吗?我确信这在 awk 中很有可能,但我不知道,如果你只是需要完成工作那么你可以尝试。但不确定该分隔符到底是什么。
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.