我是bash,awk,脚本的新手;所以请帮我改善。
我有大量的文本文件,每个文本文件的大小为几百MB。不幸的是,它们并没有以任何一种格式完全标准化。另外,这里有很多遗产,还有很多垃圾和乱码。我希望检查所有这些文件以查找具有有效电子邮件ID的行,如果存在,则将其打印到名为电子邮件ID的首字符的文件中。因此,多个文本文件被解析并组织成名为a-z和0-9的文件。如果电子邮件地址以特殊字符开头,则它将被写入下划线“ _”文件中。该脚本还修剪了行以删除空格。并替换单引号和双引号(这是应用程序的要求)
我的脚本运行正常。这里没有错误/错误。但这是非常缓慢的。我的问题:是否有更有效的方法来实现这一目标?解析30 GB的日志需要我大约12个小时-太多了! grep / cut / sed / another会更快吗?
样本txt文件
!bar@foo.com,address
#john@foo.com;address
john@foo.com;address µÖ
email1@foo.com;username;address
email2@foo.com;username
email3@foo.com,username;address [spaces at the start of the row]
email4@foo.com|username|address [tabs at the start of the row]
我的代码:
awk -F'[,|;: \t]+' '{
gsub(/^[ \t]+|[ \t]+$/, "")
if (NF>1 && $1 ~ /^[[:alnum:]_.+-]+@[[:alnum:]_.-]+\.[[:alnum:]]+$/)
{
gsub(/"/, "DQUOTES")
gsub("\047", "SQUOTES")
r=gensub("[,|;: \t]+",":",1,$0)
a=tolower(substr(r,1,1))
if (a ~ /^[[:alnum:]]/)
print r > a
else
print r > "_"
}
else
print $0 > "ErrorFile"
}' *.txt