我有一个脚本,它与来自多个日志文件的目录中的字符串匹配,如下所示:
#!/bin/sh
# Collect Customer ID as input
read -p "Enter Customer ID: " custid
echo "Searched customer ID $custid found in following logs: "
# Find the customer id as string in specified directory
find /usr/local/tomcat9/logs/ -type f -exec grep -l "$custid" {} \;
这将输出包含搜索字符串的日志文件列表。例如:
Enter Customer ID: 2001NM-100313
Searched customer ID 2001NM-100313 found in following logs:
/usr/local/tomcat9/logs/localhost_access_log.2017-10-04.txt
/usr/local/tomcat9/logs/localhost_access_log.2017-07-11.txt
/usr/local/tomcat9/logs/localhost_access_log.2017-11-02.txt
/usr/local/tomcat9/logs/localhost_access_log.2017-09-11.txt
/usr/local/tomcat9/logs/localhost_access_log.2017-08-09.txt
/usr/local/tomcat9/logs/localhost_access_log.2017-06-11.txt
我希望这个输出像列表一样:
1. /usr/local/tomcat9/logs/localhost_access_log.2017-10-04.txt
2. /usr/local/tomcat9/logs/localhost_access_log.2017-07-11.txt
3. /usr/local/tomcat9/logs/localhost_access_log.2017-11-02.txt
4. /usr/local/tomcat9/logs/localhost_access_log.2017-09-11.txt
5. /usr/local/tomcat9/logs/localhost_access_log.2017-08-09.txt
6. /usr/local/tomcat9/logs/localhost_access_log.2017-06-11.txt
它会要求输入数字 1/2/3/4/5/6 以打开该编号文件,即;如果我按 4 它将发送命令
vim /usr/local/tomcat9/logs/localhost_access_log.2017-09-11.txt
并且将在整个文件中搜索字符串“2001NM-100313”。
我的目标是从日志文件中读取包含此字符串的整行/行(字符串可能有多行),可能有多个日志文件具有此字符串和多个日期,我需要选择任何日期文件并阅读日志。