你好,我的脚本中有以下 awk 。正则表达式模式对我来说无法正常工作。我想验证可以包含字符 [az],[0-9] ,[.] ,@ 的电子邮件地址
代码
here are the sample email patterns in the input file
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
该模式是从元数据文件中提取并作为脚本参数传递的。这里的元数据行定义了电子邮件 ID 验证的模式
1~4~~~字符~Y~\"\@\.com\"~100
sh -x 运行脚本代码
val=$(
awk -F ,
-v n=4
-v 'm="*@*.com"'
-v count=0
'NR!=1 && $n !~ "^" m "$"
{
printf "%s:%s:%s\n", FILENAME, FNR, $0 > "/dev/stderr"
count++
}
END {print count}' BNC.csv
vi 脚本代码
val=$(awk -F "$sep"
-v n="$col_pos"
-v m="$col_patt"
-v count=0
'NR!=1 && $n !~ "^" m "$"
{
printf "%s:%s:%s\n", FILENAME, FNR, $0 > "/dev/stderr"
count++
}
END {print count}' $input_file
答案1
如果您正在寻找一种验证电子邮件地址的方法,FWIW,这就是我在一个旧的 awk 脚本中所拥有的内容,它可以做到这一点:
# valid addrs regexp from http://www.regular-expressions.info/email.html
# Specifically do NOT want to use [:alpha:] to drop Asian characters etc
# Added a check that we have at least 2 consecutive alphabetic characters
# both before and after the "@" to get rid of [email protected] etc. garbage
(addr ~ /^[0-9a-zA-Z._%+-]+@[0-9a-zA-Z.-]+\.[a-zA-Z]{2,}$/) &&
(addr ~ /^.*[a-zA-Z]{2}.*@.*[a-zA-Z]{2}.*\.[a-zA-Z]{2,}$/)
我确信可以将其合并为 1 个正则表达式,但我不太关心这样做,而且最终结果可能不太清楚。