我有200000个整数写在像这样的文件中
awk
我想用join
或application/pdf
脚本编写,告诉我们这个模式(从0到99)重复多少次。
答案 0 :(得分:3)
未经过战斗测试:
awk 'i++!=$0{i=$0==0?1:0}i==100{c++;i=0}END{print c}' p.txt
故障:
i++ != $0 { # Use a cursor (i) which will be compared to input
i=$0==0?1:0; # If not matched reset cursor if current line is zero then set to 1 because
# .. this means we already matched our first line. If not set to 0
i == 100 { # If Full pattern found:
c++; # add to count
i=0; # reset cursor
}
END {print c} # Print matched count
答案 1 :(得分:2)
您可以使用状态变量执行此操作,该状态变量会在模式不完整时重置。例如:
#!/usr/bin/awk -f
BEGIN {
state = -1;
count = 0;
}
/^[0-9]+$/ {
if ( $0 == ( state + 1 ) || $0 == 0 ) {
state = $0;
if ( state == 99 ) {
count++;
}
} else {
state = -1;
}
next;
}
{ state = -1; next; }
END {
print count;
}
此脚本假定awk
位于/usr/bin
(通常情况下)。您可以将脚本放在一个文件中,例如“模式”,然后像
./patterns < p.txt