我有一个包含以下数据的文件:
1,20160507057,VBATCH_20160507_00001,1000,GGG,OR1,20160507,ATP,VS12,TEST,Ver,
2,AVAILABLE,20160507T13:23:19,ver,,
2,USED,20160507T16:45:00,,12394301044,803123123314626251006
1,20160507331,VBATCH_20160507_00003,1000,GGG,OR1,20160508,ATP,Pure,vour,Test,
2,POP,20160507T16:10:27,ver,,
2,AVAILABLE,20160507T16:17:42,ver,,
1,20160507441,VBATCH_20160507_00003,1000,GGG,OR1,20160508,ATP,Pure,vour,Test,
2,POP,20160507T16:10:27,ver,,
2,AVAILABLE,20160507T16:17:42,ver,,
记录从第一行开始:
1,20160507331,VBATCH_20160507_00003,1000,GGG,OR1,20160508,ATP,Pure,vour,Test,
这是上面记录的子行:
2,POP,20160507T16:10:27,ver,,
2,AVAILABLE,20160507T16:17:42,ver,,
因此,对于每个起始行,后面都会有一些行,所以我的要求是我需要以下值:
以 available 结尾的记录的最后一行,我需要所有这些数据以及该记录的第二列(第一行)
示例:
1,20160507331,VBATCH_20160507_00003,1000,GGG,OR1,20160508,ATP,Pure,vour,Test,
2,POP,20160507T16:10:27,ver,,
2,AVAILABLE,20160507T16:17:42,ver,,
以上记录只有我应该考虑。
输出:
20160507331 Available
最佳答案
创建文件:test_script.py
import sys
with open(sys.argv[1], 'r') as f:
last_id = None
last_value = None
for line in f:
if line.startswith('1,'):
if last_id != None and last_value == 'AVAILABLE':
print last_id, last_value
last_id = line.split(',')[1]
elif line.startswith('2,'):
last_value = line.split(',')[1]
if last_id != None and last_value == 'AVAILABLE':
print last_id, last_value
然后运行cmd:python test_script.py your_file_path
希望对你有帮助。
关于linux - 在linux中搜索特定值并在未排序的数据中进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37132859/