SED,获取最后一个文件名

发布于 2024-09-30 23:58:31 字数 920 浏览 0 评论 0原文

这是一个 SED 问题。

我正在使用 Quartz Composer (Mac OSX) 脚本来:

  1. 从目录中打开最后一个 .log。
  2. 获取该文件的最后一个文本行并删除其前 24 个字符。
  3. 每次在最后一个 .log 中创建新行时显示最后一行

我认为这会很简单。目前,我已经在QC中完成了这项工作,并且几乎可以工作了。 中的 SED 命令都有问题,

但是我在 getFileName.shgetLastLineOfLog.sh getFileName.sh

#!/bin/bash

cd /Users/Me/
/bin/echo `ls -tr *.log | tail -1`

我在这里需要的是打开最后一个.log 文件,其名称结构 127.0.0.1,NUMBER.log 是 NUMBER 部分,一个 5 位数字。数字越大,.log 文件越新。现在,它仅打开第一个 .log 文件。

您知道如何在 SED 中编写正确的命令吗?

getLastLineOfLog.sh

#!/bin/bash

/usr/bin/tail -1 /Users/Me/$1 | sed 's/^.\{24\}//' > /Users/Me/$1.log
echo `/usr/bin/tail -1 /Users/Me/$1.log`

在第二个文件中,我尝试获取加载的 .log 的最后一个文件并回显它,但不包含其前 24 个字符。它有效,但创建了一个新的.log.log。 (等等)每次函数通过时都会文件,这是我想避免的事情。

是否有必要将SED命令的结果输出到新文件中?怎样才能做到尽可能简单呢?

先感谢您。

It is a SED issue.

I am working in Quartz Composer (Mac OSX) script to:

  1. Open the last .log from a directory.
  2. Getting the last text line of that file and remove its 24 first characters.
  3. Show the last line each time a new one is created inside the last .log

I think this would be so simple. At this moment, I have this done in QC, and it is almost working. But I have issues with the SED commands in both getFileName.sh and getLastLineOfLog.sh

getFileName.sh

#!/bin/bash

cd /Users/Me/
/bin/echo `ls -tr *.log | tail -1`

What I need here is to open the last .log file, with this name structure 127.0.0.1,NUMBER.log being the NUMBER section a 5 digits number. The higher the number, the newer the .log file. Now, it opens just the first .log file.

Do you know how to write in SED the right commands for doing that, please?

getLastLineOfLog.sh

#!/bin/bash

/usr/bin/tail -1 /Users/Me/$1 | sed 's/^.\{24\}//' > /Users/Me/$1.log
echo `/usr/bin/tail -1 /Users/Me/$1.log`

In this second file, I have tried to get the last file of the loaded .log and echo it without its 24 first characters. It works, but creating a new .log.log. (etc.) file each time the function passes, thing that I would like to avoid.

Is it necessary to output the results of the SED commands to a new file? How could it be done as simple as possible?

Thank you in advance.

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

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

发布评论

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

评论(3

终难遇 2024-10-07 23:58:31

如果你想按文件名排序:

ls *.log | sort -t, -k2 -n | tail -1

if you want to sort by the filename:

ls *.log | sort -t, -k2 -n | tail -1
野生奥特曼 2024-10-07 23:58:31

getFileName.sh

#!/bin/bash

cd /Users/Me/
ls -tr *.log | tail -1

您不需要 echo,因为 echo 会输出其参数。

getLastLineOfLog.sh

#!/bin/bash

/usr/bin/tail -1 /Users/Me/$1 | sed 's/^.\{24\}//' 

与此类似,您已经只有最后一行,因此不需要另一个 tail

编辑:
事实上,您也可以通过在 sed 中执行以下操作来摆脱尾部:

getLastLineOfLog.sh

#!/bin/bash

sed -ne'$s/^.\{24\}//p' /Users/Me/$1

getFileName.sh

#!/bin/bash

cd /Users/Me/
ls -tr *.log | tail -1

You don't need the echo, as that echo outputs its arguments.

getLastLineOfLog.sh

#!/bin/bash

/usr/bin/tail -1 /Users/Me/$1 | sed 's/^.\{24\}//' 

Similarly here, you already have just the last line, so you don't need another tail.

EDIT:
In fact you can get rid of the tail as well, by doing it in sed:

getLastLineOfLog.sh

#!/bin/bash

sed -ne'$s/^.\{24\}//p' /Users/Me/$1
若水般的淡然安静女子 2024-10-07 23:58:31

我知道这是一个非常老的问题,但这里有两种方法可以仅以 bash 方式且不使用管道来获取最后一个文件:

  • 第一种方法:

    <前><代码>:* &&回声$_

    man bash 说:

    <块引用>

    $_ 在扩展后扩展为上一个命令的最后一个参数。

    因此,您在这里执行命令 : (不执行任何操作),并将所有文件作为参数,然后使用 $_ 获取最后一个文件。

  • 第二种方法:

    对于 * 中的最后一个文件;做 : ;完毕
    回显“$lastfile”
    

    通过这种方式,您除了将每个文件分配给 lastfile 之外什么也不做,因此,最后,您将把最后一个文件(按字母顺序排列)保存在lastfile

I know it's a very old question, but here are two ways to get the last file in a bash only way and without pipes:

  • First method:

    : * && echo $_
    

    The man bash says:

    $_ expands to the last argument to the previous command, after expansion.

    So here you're executing the command : (does nothing) with all the files as arguments and then you're getting the last of them with $_.

  • Second method:

    for lastfile in *; do : ; done
    echo "$lastfile"
    

    In this way you're doing nothing but assigning each file to lastfile so, at the end, you'll have the last file (alphabetically talking) saved in lastfile.

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