用于在一系列文件中进行 grep 的 Shell 脚本

我正在设置一个脚本,该脚本通过read命令获取一些用户数据。使用这些数据,我需要搜索文件范围,然后进行一些过滤。这是它的样子,


Enter fromtime


read fromtime


Enter totime


read totime


Enter the ID


read id

最初我通过 SSH 连接到服务器,然后在那里我有一个目录,cd home/report/records这里有路径记录,我有:

  • REC_201901020345.gz (yyyymmddhhmm)

  • REC_201901120405.gz

  • REC_201903142543.gz

等等。

这些文件包含数据以及$id.

当用户输入时$fromtime$totime它将是 yyyymmddhh 格式。在这里,我需要转到该范围的文件,然后grep进行$id显示。例如:

如果$fromtime2019010103并且$totime2019031425。我只需要转到那些特定范围的文件,即REC_201901020345.gzREC_201901120405.gzREC_201903142543.gz并执行grep以查找id用户输入的内容。

我已经使用if条件尝试过这个,但它似乎不起作用。我是编写此类脚本的新手。当我在这里描述所有内容时可能会有错误。对不起。


source config.sh


Enter fromtime


read fromtime


Enter totime


read totime


Enter the ID


read id


ssh $user@$ip



cd /home/report/records


# <-- need to know what to add here as described here, to navigate to the

# <-- specific range $fromtime-$totime. Then the command to find id will be 


zfgrep $id *.gz

结果应该只是id指定.gz文件范围内带有' 的数据。


狐的传说
浏览 136回答 2
2回答

湖上湖

试试下面的命令。echo&nbsp;-e&nbsp;"$(ls&nbsp;-1&nbsp;REC_????????????.gz&nbsp;2>/dev/null)\nREC_${fromtime}##\nREC_${totime}##"&nbsp;|&nbsp;sort&nbsp;|&nbsp;sed&nbsp;-n&nbsp;"/##/,/##/p"&nbsp;|&nbsp;sed&nbsp;'1d;$d'&nbsp;|&nbsp;xargs&nbsp;zfgrep&nbsp;-a&nbsp;"$id"解释:'fromdate' 和 'todate' 连同##(比如标记)被附加到ls.对输入进行排序,从而得到用标记括起来的所需文件名。Both&nbsp;sed,仅打印标记之间的线条。最后一个是命令,应该为每个文件名执行。您可以省略管道和所有下一个命令,从结尾开始,并查看输出是如何构建的。

哈士奇WWW

要获取给定范围(fromtime、totime)内的文件列表,可以使用以下 shell 脚本:declare -i tafor file in REC*.gzdo&nbsp;&nbsp; &nbsp; ta=$(echo "${file}" | grep -oP 'REC_\K(.*)(?=[[:digit:]]{2}.gz)')&nbsp; &nbsp; if [ "${ta}" ] ; then&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if [ ${ta} -le ${totime} -a ${ta} -ge ${fromtime} ] ; then&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo -e "${file}"&nbsp; &nbsp; &nbsp; &nbsp;fi&nbsp; &nbsp; fi&nbsp;&nbsp;done
打开App,查看更多内容
随时随地看视频慕课网APP