如何在Shell脚本中使用GREP?

发布于 2025-01-26 02:06:47 字数 407 浏览 1 评论 0原文

  • 我正在尝试制作一个外壳脚本,该脚本打印出以下文件的最后一个修改日期。
  • 脚本以某种方式仅打印出一个空行
  • “修改”是一个文件,其中包含以下格式的名称和修改日期(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2025-02-02 02:06:47

正如查尔斯所提到的那样,没有理由为此创建修改的文件(除非您打算将该文件用于其他目的)。

另外,您可以为stat命令提供不同的参数,如:

stat --printf='%y\n' file1 file2 file3

这给出与您的目标完全相同的输出。

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:

stat --printf='%y\n' file1 file2 file3

This gives exactly the same output as what you're aiming for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文