【问题标题】:grep for substrings present for each string in file - shell scriptgrep 查找文件中每个字符串存在的子字符串 - shell 脚本
【发布时间】:2016-11-10 14:05:17
【问题描述】:

我在这样的文件中有一个字符串:

 47:2016-10-25 20:24:21 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
 301:2016-10-26 08:01:01 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s

我有一个输入字符串为日期“2016-10-25”,另一个输入字符串为“处理身份请求。发送身份响应”。

我想找到号码。文件中存在的字符串。我试过这个:

  total_attempt=$(grep -c "$i\|\[HSM \]Handle Identity Request. Send 
  Identity Response. timeout: 1550s" $file_name

我相信 |在上述语法中将不起作用。

注意:$i 是输入的日期

输出应该是这样的:

 2016-10-25: = attempts = 2 

但这不起作用。

Sed 命令会有所帮助,但我不确定如何?

【问题讨论】:

    标签: shell grep


    【解决方案1】:

    你可以试试这个吗?

    #!/bin/bash
    file_name=yourFile
    i="2016-10-25"
    total_attempt=$(grep -c "$i\|\[HSM \]Handle Identity Request. Send Identity Response. timeout: 1550s" $file_name)
    echo $total_attempt
    

    【讨论】:

      【解决方案2】:

      要使| 工作,您需要打开扩展正则表达式,即将-E 开关传递给grep。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多