我有一个数字列表,如下所示:
1 0/1
2 1/1
3 1/1
4 1/1
5 1/1
6 1/1
7 0/1
8 0/1如果第二列对于连续的行是"1/1“,我想报告位置的开始和结束,例如,在这里,它应该是: 2-6
我应该如何应用一些简单的bash代码,或者如果需要的话,应用python?
非常感谢
发布于 2012-11-06 05:40:29
如果你能用python编写代码,你可以用下面的方法来解决:
因此,代码将如下所示:
import re
# step 1
with open('filename') as f:
data = f.read()
# step 2
list = re.findall(r'(\d+)\s+1/1', data)
# step 3
# Check the link in the description of the algorithm发布于 2012-11-06 06:01:45
Bash解决方案:
#! /bin/bash
unset in # Flag: are we inside an interval?
unset last # Remember the last position.
while read p f ; do
if [[ $f = 1/1 && ! $in ]] ; then # Beginning of an interval.
echo -n $p-
in=1
elif [[ $f = 1/1 && $in ]] ; then # Inside of an interval.
last=$p
elif [[ $f != 1/1 && $in ]] ; then # End of an interval.
echo $last
unset in
fi
donehttps://stackoverflow.com/questions/13240564
复制相似问题