2016-11-10 144 views
1

我有一个文件中的字符串是这样的:grep命令目前在文件中的每串子 - shell脚本

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命令将有所帮助,但我不知道如何?

回答

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 
0

对于|工作,你需要打开扩展正则表达式,即也-E开关传递给grep的。