awk根据条件过滤文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了awk根据条件过滤文件相关的知识,希望对你有一定的参考价值。
我使用下面的代码来过滤我的源文件。此代码包含多个gsub条件
但是这里的问题是由于多个gsub,脚本需要花费大量时间来执行和解析另一个文件中的数据。你能不能让我知道实现这些条件的其他方法,以便我的脚本可以更快地执行
awk -F"[ ]" -v OFS="|" '{gsub(/^[ ]+|[ ]+$/,"");gsub(/"/,"");gsub(/[[:blank:]]+|.|-/,"",$32);gsub(/[[:blank:]]+|.|-/,"",$13);gsub(/^[[:space:]]+|[[:spa
ce:]]+$|,/,"",$38);gsub(/^[[:space:]]+|[[:space:]]+$|,/,"",$42);gsub(/^[[:space:]]+|[[:space:]]+$|,/,"",$44);gsub(/^[[:space:]]+|[[:space:]]+$/,"",$27);gsub(/^[[:spa
ce:]]+|[[:space:]]+$/,"",$40);gsub(/^[[:blank:]]+|[[:blank:]]+$/,"",$18);
下面是相同的代码,但是为了便于阅读而切片(仅限可读性):
awk -F"[ ]" -v OFS="|" '{gsub(/^[ ]+|[ ]+$/,"");
gsub(/"/,"");
gsub(/[[:blank:]]+|.|-/,"",$32);
gsub(/[[:blank:]]+|.|-/,"",$13);
gsub(/^[[:space:]]+|[[:space:]]+$|,/,"",$38);
gsub(/^[[:space:]]+|[[:space:]]+$|,/,"",$42);
gsub(/^[[:space:]]+|[[:space:]]+$|,/,"",$44);
gsub(/^[[:space:]]+|[[:space:]]+$/,"",$27);
gsub(/^[[:space:]]+|[[:space:]]+$/,"",$40);
gsub(/^[[:blank:]]+|[[:blank:]]+$/,"",$18);
如果需要输入文件,请告诉我,将分享
答案
这不是答案,而是代码的可读版本:
awk -F"[ ]" -v OFS="|" '
{ gsub(/^[ ]+|[ ]+$/,"");
gsub(/"/,"");
gsub(/[[:blank:]]+|.|-/,"",$32);
gsub(/[[:blank:]]+|.|-/,"",$13);
gsub(/^[[:space:]]+|[[:space:]]+$|,/,"",$38);
gsub(/^[[:space:]]+|[[:space:]]+$|,/,"",$42);
gsub(/^[[:space:]]+|[[:space:]]+$|,/,"",$44);
gsub(/^[[:space:]]+|[[:space:]]+$/,"",$27);
gsub(/^[[:space:]]+|[[:space:]]+$/,"",$40);
gsub(/^[[:blank:]]+|[[:blank:]]+$/,"",$18);
}' # closing brace and quote were probably missing
以上是关于awk根据条件过滤文件的主要内容,如果未能解决你的问题,请参考以下文章