改进了将文件名存储在变量中的技术?

发布于 2024-08-25 16:21:27 字数 739 浏览 1 评论 0原文

您好,

我需要将日志的文件名存储到变量中,以便我的脚本可以对每日日志文件执行一些检查。这些日志始终具有不同的名称,因为它们的名称中包含时间戳。目前,我正在使用一种 Hodge podged 方法,该方法将 ls 命令通过管道传输到 sed、sort、cut 和 tail,以便获取名称。

CRON_LOG=$(ls -1 $LOGS_DIR/fetch_cron_{true,false}_$CRON_DATE*.log 2> /dev/null | sed 's/^[^0-9][^0-9]*\([0-9][0-9]*\).*/\1 &/' | sort -n | cut -d ' ' -f2- | tail -1 )

更新:

$CRON_DATE 作为脚本的参数提供。它是创建日志的日期(截止日期)。有时同一天会存在多个日志,因此我希望获得最新的日志。

一些典型的文件名:
fetch_cron_false_031810090452.log
fetch_cron_true_031310090001.log
等等...

请记住,这是按原样工作的。我只是觉得这很丑陋,正在努力寻找更好的方法来实现它。

我很确定我是从几个月前在谷歌上找到的一些东西中拼凑出来的。它现在可以工作了,但我对这项技术不太满意。我对如何做得更好有一些想法,但我之前在这个网站上取得了巨大的成功,并且认为最好先参考 stackoverflow 的大神。非常感谢所有答案。

谢谢, 瑞安

Greetings,

I need to store the filename of a log into a variable so my script can perform some checks on the daily log files. These logs always have a different name because they have a timestamp in the name. Currently I'm using a hodge podged method that pipes an ls command to sed, sort, cut, and tail in order to get the name out.

CRON_LOG=$(ls -1 $LOGS_DIR/fetch_cron_{true,false}_$CRON_DATE*.log 2> /dev/null | sed 's/^[^0-9][^0-9]*\([0-9][0-9]*\).*/\1 &/' | sort -n | cut -d ' ' -f2- | tail -1 )

UPDATE:

$CRON_DATE is supplied as an argument to the script. It is the date (to the day) that the log was created on. Sometimes multiple logs will exist for the same day so I want this to get the most recent one.

Some typical filenames:
fetch_cron_false_031810090452.log
fetch_cron_true_031310090001.log
etc...

Please keep in mind that this works as is. I just think it is ugly and am trying to find a better way to pull it off.

I'm pretty sure I kluged this together from some stuff I found google a few months ago. it works now but I'm not really happy with the technique. I have some ideas about how to do this better but I have had great success on this site before and thought it might be best to refer to the stackoverflow gods first. All answers are greatly appreciated.

Thanks,
Ryan

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

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

发布评论

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

评论(5

浮萍、无处依 2024-09-01 16:21:27

编辑

CRON_LOG=$(ls -c $LOGS_DIR/fetch_cron_*$CRON_DATE* | head -1)

:你是对的,我应该明白这一点。固定的。

How about

CRON_LOG=$(ls -c $LOGS_DIR/fetch_cron_*$CRON_DATE* | head -1)

EDIT: You're right bta, I should have caught that. Fixed.

小草泠泠 2024-09-01 16:21:27

解析日志文件后,执行一些操作来标记它已被处理,例如重命名它或将其移动到其他文件夹。这样,文件何时创建并不重要,只是文件尚未被解析而已。

我还建议使用更高级的脚本语言。

After you parse a log file, do something to mark that it has been processed, such a renaming it or moving it to a different folder. This way it won't matter when the file was created, only that it hasn't been parsed yet.

I would also suggest using a more high level scripting language.

若沐 2024-09-01 16:21:27
#!/bin/bash
shopt -s nullglob

last=
for file in "$LOGS_DIR"/fetch_cron_{false,true}_"$CRON_DATE"*.log
do
  last="$file"
done

if [ -n "$last" ]
then
  echo "$last"
else
  echo "No match found" >&2
  exit 1
fi
#!/bin/bash
shopt -s nullglob

last=
for file in "$LOGS_DIR"/fetch_cron_{false,true}_"$CRON_DATE"*.log
do
  last="$file"
done

if [ -n "$last" ]
then
  echo "$last"
else
  echo "No match found" >&2
  exit 1
fi
黯然#的苍凉 2024-09-01 16:21:27

对于初学者,请尝试:

FILELIST=`ls -1 $LOGS_DIR/fetch_cron_{true,false}_$CRON_DATE*.log`
CRON_LOG=`echo $FILELIST | tr -d [:alpha:][:punct:] | sort -n | tail -1`

摆脱 cut 并将 sed 正则表达式换成对 tr 的更具可读性 (IMO) 的调用。将其分成两行也有助于提高清晰度。

如果您在 $LOGS_DIR 中没有任何其他文件名极其相似的文件(如果您将相关日志保留在其自己的文件夹中,通常会发生这种情况),您可以将该参数替换为 ls 使用更简单的内容,例如 $LOGS_DIR/fetch_*_$CRON_DATE*.log。为了简单起见,不要使该行比所需的更复杂,以确保您只获得所需的文件。

For starters, try:

FILELIST=`ls -1 $LOGS_DIR/fetch_cron_{true,false}_$CRON_DATE*.log`
CRON_LOG=`echo $FILELIST | tr -d [:alpha:][:punct:] | sort -n | tail -1`

That gets rid of cut and trades the sed regular expression for a more-readable (IMO) call to tr. Splitting it into two lines helps with the clarity as well.

If you don't have any other files in $LOGS_DIR with extremely similar filenames (which usually happens if you keep the logs in question in their own folder), you can replace the parameter to ls with something simpler like $LOGS_DIR/fetch_*_$CRON_DATE*.log. For the sake of simplicity, don't make that line more complicated than required to ensure you get only the files you need.

九八野马 2024-09-01 16:21:27

您可以通过在前面加上年份前缀来将日期更改为可排序,然后执行

CRON_LOG=$(ls ${LOGS_DIR}/fetch_cron_{false,true}_${CRON_DATE}*.log| tail -1)

you can change your date to sortable by prefixing the year in front, then do

CRON_LOG=$(ls ${LOGS_DIR}/fetch_cron_{false,true}_${CRON_DATE}*.log| tail -1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文