linux - 在linux中搜索特定值并在未排序的数据中进行计数

标签 linux shell scripting

我有一个包含以下数据的文件:

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/

相关文章:

linux - bash - 删除目录列表(包括它们的内容)

bash - 如何检查 bash 脚本是否正在运行并退出

text - 在 python 中查找许多元素中的第一个 - text.find(a , b , c)

linux - 为什么程序没有执行这个C编程或unix编程中的某些语句(execvp()系统调用)?

windows - 通用调用约定如何处理 AVX 寄存器?

c - execvp() 在我的 shell 中不工作

powershell - 将参数传递到 Powershell

linux - 如何使用终端在 Linux 中创建仅包含 .txt 扩展名的文件的 zip 存档

不同选项卡的 Linux shell 脚本

bash - 如何使用 getopt(s) 作为在 bash 中传递参数的技术