问题描述
如何判断第1和第14位之间的零的个数?通过使用grep。
How to determine the count of zeros between the 1st and 14th digit? By using grep.
500000000000922801808800000000000000000000922891863600000GS5 00*HOME
500000000000022811740000000000000000000000922811741600000GS5 00*HOME
我的条件是:如果zero_count = 12然后将记录dst_dir
其他移动记录到drp_dir
My condition is: if zero_count=12 then move the record to dst_dir else move the record to drp_dir
推荐答案
假设 dst_dir
和 drp_dir
两个要创建输出文件,以下 AWK
程序会为你做的。
Assuming that dst_dir
and drp_dir
are two output files you want to create, the following awk
program will do that for you.
awk '
NF {
data = substr ($0, 1, 14)
count = gsub (/0/, "", data)
print > (count==12 ? "dst_dir" : "drp_dir")
}' file
使用 NF
我们跳过空白行。我们创建一个使用您的行 SUBSTR
功能的子集。 GSUB
返回做替代品的数量,所以我们捕捉返回值变量计数
。
Using NF
we skip the blank lines. We create a subset of your line using substr
function. gsub
returns the number of substitution made, so we capture the return value to variable count
.
最后,我们测试是否计数
是12。如果是我们写的记录 dst_dir
否则我们写记录 drp_dir
。
Lastly, we test if the count
is 12. If it is we write the record to dst_dir
else we write the record to drp_dir
.
这篇关于通过使用grep数第1和第14位之间的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!