如何在Shell脚本中使用GREP?
- 我正在尝试制作一个外壳脚本,该脚本打印出以下文件的最后一个修改日期。
- 脚本以某种方式仅打印出一个空行
- “修改”是一个文件,其中包含以下格式的名称和修改日期(DeLimiter ='@'):
>modified
for i in file1 file2 file3
do
echo $i@`stat --printf='%y\n' $i`>>modified
done
创建该文件后,我正在尝试搜索它喜欢:
for i in file1 file2 file3
do
var=`grep -w "$i" modified | cut -d'@' -f2`
echo $var
done
- I am trying to make a shell script which prints out the last modification dates of the following files.
- Somehow the script just prints out an empty line
- "modified" is a file which contains the names and the modification dates of the files in the following format(delimiter='@'):
>modified
for i in file1 file2 file3
do
echo $i@`stat --printf='%y\n' $i`>>modified
done
Having created that file, I'm trying to search it like:
for i in file1 file2 file3
do
var=`grep -w "$i" modified | cut -d'@' -f2`
echo $var
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如查尔斯所提到的那样,没有理由为此创建
修改的
文件(除非您打算将该文件用于其他目的)。另外,您可以为
stat
命令提供不同的参数,如:这给出与您的目标完全相同的输出。
As mentioned by Charles, there's no reason to create that
modified
file for that (unless you are planning to use that file for another purpose).Also, you can give different arguments to your
stat
command, as in:This gives exactly the same output as what you're aiming for.