我有2个相同的.txt文件,其中命名中有1个零件相同,而其他部分则不同。我想根据以下条件打印文件

发布于 2025-02-12 19:48:12 字数 749 浏览 1 评论 0原文

  • 文件名1:alert_human _*。txt
  • 文件名称2:alert_human_abc _*。txt

*表示

我试图将这些文件名写入<<<<< Code> code> sublefiles.txt 通过使用我保存在不同的不同的shell脚本搜索文件,将人/temp目录放在中目录(人/STG)。

注意我仅保留alert_human _*。但是它将该文件写为alert_human_abc _*。txt

因此,根据以下条件,我想打印文件名。

  1. alert_human_abc _*。txt - 如果存在此文件,则应仅打印此文件名。
  2. alert_human _*。txt - 如果存在此文件,则应仅打印此文件名,该文件名不应将其打印为第一个提到的文件名。

我曾经像

echo 'alert_human_abc_*.txt > temp/availablefiles.txt
echo 'alert_human_*.txt > temp/availablefiles.txt
  • file name 1 : alert_human_*.txt
  • file name 2 : alert_human_abc_*.txt

* denotes the timestamp

I am trying to write these filenames to availablefiles.txt file which is placed in human/temp directory by searching the files using a shell script that I have kept in different directory (human/stg).

Note I have kept only alert_human_*.txt in human/stg directory. But it writes that file as alert_human_abc_*.txt.

So based on below condition I want to print the filenames.

  1. alert_human_abc_*.txt - if this file is present, it should print only this filename.
  2. alert_human_*.txt - if this file is present, it should print only this filename which should not print as 1st mentioned file name.

I have used like

echo 'alert_human_abc_*.txt > temp/availablefiles.txt
echo 'alert_human_*.txt > temp/availablefiles.txt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

橘味果▽酱 2025-02-19 19:48:12

您的问题不太清楚,但这里是。

#!/bin/bash

outputfile='human/temp/availablefiles.txt'

abc_filename="alert_human_abc_*.txt"
noabc_filename="alert_human_*.txt"
if [[ $(find . -name "$abc_filename") ]]
then
    echo "$abc_filename" >"$outputfile"
else
    echo "$noabc_filename" >"$outputfile"
fi
  • 如果任何文件匹配alert_human_abc _*。txt,则将匹配模式写入文件。
  • 否则,图案alert_human _*。txt是编写的。

如果您想要与输出文件中每个模式匹配的文件列表,请这样修改:

#!/bin/bash

outputfile='human/temp/availablefiles.txt'

abc_filename="alert_human_abc_*.txt"
noabc_filename="alert_human_*.txt"
if [[ $(find . -name "$abc_filename") ]]
then
    find . -name "$abc_filename" >"$outputfile"
else
    find . -name "$noabc_filename" >"$outputfile"
fi
  • 而不是编写模式,而是在输出文件中使用该模式编写find的结果。

Your question is not super clear, but here goes.

#!/bin/bash

outputfile='human/temp/availablefiles.txt'

abc_filename="alert_human_abc_*.txt"
noabc_filename="alert_human_*.txt"
if [[ $(find . -name "$abc_filename") ]]
then
    echo "$abc_filename" >"$outputfile"
else
    echo "$noabc_filename" >"$outputfile"
fi
  • If any file matches alert_human_abc_*.txt, that matching pattern is written to the file.
  • Otherwise, the pattern alert_human_*.txt is written.

If you wanted the list of files that match each pattern in your output file, modify like this:

#!/bin/bash

outputfile='human/temp/availablefiles.txt'

abc_filename="alert_human_abc_*.txt"
noabc_filename="alert_human_*.txt"
if [[ $(find . -name "$abc_filename") ]]
then
    find . -name "$abc_filename" >"$outputfile"
else
    find . -name "$noabc_filename" >"$outputfile"
fi
  • Instead of writing the pattern, write the result of find using that pattern to the output file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文