使用 grep 计算第 1 位和第 14 位数字之间的零数 [英] count of zeros between the 1st and 14th digit by using grep

查看:24
本文介绍了使用 grep 计算第 1 位和第 14 位数字之间的零数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定第 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_dirdrp_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 返回替换的次数,因此我们将返回值捕获到变量 count 中.

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.

最后,我们测试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 位数字之间的零数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆