grep表示文件 - shell脚本中每个字符串的子字符串

时间:2016-11-10 14:05:17

标签: shell grep

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

 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命令会有所帮助,但我不确定如何?

2 个答案:

答案 0 :(得分:0)

你可以尝试这个吗?

#!/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

答案 1 :(得分:0)

要使|生效,您需要启用扩展正则表达式,即也将-E开关传递给grep。