搜索并删除带有可变参数的字符串

时间:2015-09-30 14:46:47

标签: macos bash perl sed terminal

我需要在OSX上使用命令行从文本文件中删除字符串。

该字符串将具有一些永久性功能,但具有任意数字。

我想删除以下各种变体: -

            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]    

此字符串具有不同的值,但我想将其删除,只留下标签部分中的相关信息: -

 PAttern1 :
 Code :
 CodeHash :
 Result :

需要执行以下MOCK程序

 for each line{
if string contains [%d%d%d.%d%d Kkey/s][total %d%d%d%d%d%d%d][Prob %d.%d%][%d%d% in %d.%dh]  (where "%d" is decimal value){

delete string and line

}
else if !contains {

 move down 1 line.
}
{

使用以下文本,只有模式和哈希值应保存到文本文档

            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]
            [370.02 Kkey/s][total 3000368][Prob 0.0%][50% in 7.8h]
            [323.02 Kkey/s][total 3001368][Prob 0.0%][50% in 7.8h]
            [389.02 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [379.06 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [300.01 Kkey/s][total 3004368][Prob 0.0%][50% in 7.8h]
            [379.02 Kkey/s][total 3005368][Prob 0.0%][50% in 7.8h]
            [279.02 Kkey/s][total 3006368][Prob 0.0%][50% in 7.8h]
            [179.02 Kkey/s][total 3007898][Prob 0.0%][50% in 7.8h]



            PAttern1: i want to keep all the text after pattern and all the code until the next line of processing including hash
            Code : aksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsd

            CodeHash : e3287cb701f336a628885e66d17e38f2

            Result : D0B6789D4E0ACF39A8C24B424C0060FFFF2DA857



            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]
            [370.02 Kkey/s][total 3000368][Prob 0.0%][50% in 7.8h]
            [323.02 Kkey/s][total 3001368][Prob 0.0%][50% in 7.8h]
            [389.02 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [379.06 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [300.01 Kkey/s][total 3004368][Prob 0.0%][50% in 7.8h]
            [379.02 Kkey/s][total 3005368][Prob 0.0%][50% in 7.8h]
            [279.02 Kkey/s][total 3006368][Prob 0.0%][50% in 7.8h]
            [179.02 Kkey/s][total 3007898][Prob 0.0%][50% in 7.8h]
            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]
            [370.02 Kkey/s][total 3000368][Prob 0.0%][50% in 7.8h]
            [323.02 Kkey/s][total 3001368][Prob 0.0%][50% in 7.8h]
            [389.02 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [379.06 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [300.01 Kkey/s][total 3004368][Prob 0.0%][50% in 7.8h]
            [379.02 Kkey/s][total 3005368][Prob 0.0%][50% in 7.8h]
            [279.02 Kkey/s][total 3006368][Prob 0.0%][50% in 7.8h]
            [179.02 Kkey/s][total 3007898][Prob 0.0%][50% in 7.8h]
            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]
            [370.02 Kkey/s][total 3000368][Prob 0.0%][50% in 7.8h]
            [323.02 Kkey/s][total 3001368][Prob 0.0%][50% in 7.8h]
            [389.02 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [379.06 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [300.01 Kkey/s][total 3004368][Prob 0.0%][50% in 7.8h]
            [379.02 Kkey/s][total 3005368][Prob 0.0%][50% in 7.8h]
            [279.02 Kkey/s][total 3006368][Prob 0.0%][50% in 7.8h]
            [179.02 Kkey/s][total 3007898][Prob 0.0%][50% in 7.8h]
            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]
            [370.02 Kkey/s][total 3000368][Prob 0.0%][50% in 7.8h]
            [323.02 Kkey/s][total 3001368][Prob 0.0%][50% in 7.8h]
            [389.02 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [379.06 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [300.01 Kkey/s][total 3004368][Prob 0.0%][50% in 7.8h]
            [379.02 Kkey/s][total 3005368][Prob 0.0%][50% in 7.8h]
            [279.02 Kkey/s][total 3006368][Prob 0.0%][50% in 7.8h]
            [179.02 Kkey/s][total 3007898][Prob 0.0%][50% in 7.8h]

            PAttern2: i want to keep all the text after pattern and all the code until the next line of processing including hash
            Code : qwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrq

            CodeHash : 05bfabd43ff6c85a8fa2d3347d828c4b

            Result : 165B6CA902E567959C9BB599FABEB3998B21E31F


            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]
            [370.02 Kkey/s][total 3000368][Prob 0.0%][50% in 7.8h]
            [323.02 Kkey/s][total 3001368][Prob 0.0%][50% in 7.8h]
            [389.02 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [379.06 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [300.01 Kkey/s][total 3004368][Prob 0.0%][50% in 7.8h]
            [379.02 Kkey/s][total 3005368][Prob 0.0%][50% in 7.8h]
            [279.02 Kkey/s][total 3006368][Prob 0.0%][50% in 7.8h]
            [179.02 Kkey/s][total 3007898][Prob 0.0%][50% in 7.8h]
            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]
            [370.02 Kkey/s][total 3000368][Prob 0.0%][50% in 7.8h]
            [323.02 Kkey/s][total 3001368][Prob 0.0%][50% in 7.8h]
            [389.02 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [379.06 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [300.01 Kkey/s][total 3004368][Prob 0.0%][50% in 7.8h]
            [379.02 Kkey/s][total 3005368][Prob 0.0%][50% in 7.8h]
            [279.02 Kkey/s][total 3006368][Prob 0.0%][50% in 7.8h]
            [179.02 Kkey/s][total 3007898][Prob 0.0%][50% in 7.8h]
            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]
            [370.02 Kkey/s][total 3000368][Prob 0.0%][50% in 7.8h]
            [323.02 Kkey/s][total 3001368][Prob 0.0%][50% in 7.8h]
            [389.02 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [379.06 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [300.01 Kkey/s][total 3004368][Prob 0.0%][50% in 7.8h]
            [379.02 Kkey/s][total 3005368][Prob 0.0%][50% in 7.8h]
            [279.02 Kkey/s][total 3006368][Prob 0.0%][50% in 7.8h]
            [179.02 Kkey/s][total 3007898][Prob 0.0%][50% in 7.8h]
            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]
            [370.02 Kkey/s][total 3000368][Prob 0.0%][50% in 7.8h]
            [323.02 Kkey/s][total 3001368][Prob 0.0%][50% in 7.8h]
            [389.02 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [379.06 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [300.01 Kkey/s][total 3004368][Prob 0.0%][50% in 7.8h]
            [379.02 Kkey/s][total 3005368][Prob 0.0%][50% in 7.8h]
            [279.02 Kkey/s][total 3006368][Prob 0.0%][50% in 7.8h]
            [179.02 Kkey/s][total 3007898][Prob 0.0%][50% in 7.8h]
            [370.15 Kkey/s][total 2999999][Prob 0.0%][50% in 7.8h]
            [370.02 Kkey/s][total 3000368][Prob 0.0%][50% in 7.8h]
            [323.02 Kkey/s][total 3001368][Prob 0.0%][50% in 7.8h]
            [389.02 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [379.06 Kkey/s][total 3003368][Prob 0.0%][50% in 7.8h]
            [300.01 Kkey/s][total 3004368][Prob 0.0%][50% in 7.8h]
            [379.02 Kkey/s][total 3005368][Prob 0.0%][50% in 7.8h]
            [279.02 Kkey/s][total 3006368][Prob 0.0%][50% in 7.8h]
            [179.02 Kkey/s][total 3007898][Prob 0.0%][50% in 7.8h]

将生成的文本文档保留为: -

            PAttern1 : i want to keep all the text after pattern and all the code until the next line of processing including hash
            Code : aksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsdaksjhdakjsdhakshdkasdhkashkjahsd
            CodeHash : e3287cb701f336a628885e66d17e38f2
            Result : D0B6789D4E0ACF39A8C24B424C0060FFFF2DA857
            PAttern2 : i want to keep all the text after pattern and all the code until the next line of processing including hash
            Code : qwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrqgfqwfufiyfoqywfofqwytrq
            CodeHash: 05bfabd43ff6c85a8fa2d3347d828c4b
            Result : 165B6CA902E567959C9BB599FABEB3998B21E31F

我使用sed删除单个字符,如下所示: -

 sed -i '' 's/%/\ /g' file.txt 
 sed -i '' 's/\[/\ /g' file.txt
 sed -i '' 's/]/\ /g' file.txt
 sed -i '' 's/Kkey\/s/\ /g' file.txt

虽然这适用于符号我不能猜测数字或sed out数字,因为它们在重要的字符串内,sed取代它!

考虑到上述例程,您将如何处理这个问题?

非常感谢您提供的任何帮助

2 个答案:

答案 0 :(得分:1)

您可以将grep-v

一起使用
grep -v 'Kkey/s.*total.*Prob' < input > output

或者如果你想删除空行:

egrep '^[ \t]*(PAttern1|Code|CodeHash|Result) : ' < input > output

答案 1 :(得分:0)

Perl命令行解决方案:

perl -ne 'print if /\S/ &&  !m|\[\d+\.\d+ Kkey/s]\[total \d+]\[Prob \d+\.\d+%]\[\d+% in \d+\.\d+h]|' your_input_file > your_output_file