使用 python 你可以写这样的东西:
import itertools
filename = "myfile"
length = 4
with open(filename, 'r') as f:
out = ''
# get your input character by character
for c in itertools.chain.from_iterable(f):
# append it to your output buffer
out += c
# if your buffer is more than N characters, remove the first char
if len(out) > length:
out = out[1:]
# if your buffer is exactly N characters, print it out (or do something else)
if len(out) is length:
print out
# if the last iteration was less than N characters, print it out (or do something else)
if len(out) < length:
print out
其中 file 是一个包含字符串完整路径的字符串。您也可以使用raw_input()
代替open()/read()
. 使用 awk 肯定有一个巧妙的解决方案,但我需要 RTFM 来告诉你如何去做。
无论您的解决方案是什么,此算法都是一种很好的方法,因为您始终只为缓冲区保留最多 N+1 个字符,再加上一个用于新读取的字符。所以这个算法的复杂度O(n)
与输入字符流是线性的( )。