我正在学习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 /”。我知道我可以用一个非正则表达式替换搜索模式,就像在第一个脚本中一样,但是假设我想用正则表达式来完成,因为我需要前缀真正处于lastName字段的开头,因为它应该是,作为前缀和所有;-)
我该怎么做?
清理:摆脱所有的空语句(尾随分号),变化的printf“的\ n “来简单地打印”“,并将printf $ 1”“$ 2等改为简单地打印$ 1,$ 2等。 – 2012-11-13 18:12:30