SED,获取最后一个文件名
这是一个 SED 问题。
我正在使用 Quartz Composer (Mac OSX) 脚本来:
- 从目录中打开最后一个 .log。
- 获取该文件的最后一个文本行并删除其前 24 个字符。
- 每次在最后一个 .log 中创建新行时显示最后一行
我认为这会很简单。目前,我已经在QC中完成了这项工作,并且几乎可以工作了。 中的 SED 命令都有问题,
但是我在 getFileName.sh 和 getLastLineOfLog.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:
- Open the last .log from a directory.
- Getting the last text line of that file and remove its 24 first characters.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想按文件名排序:
if you want to sort by the filename:
getFileName.sh
您不需要
echo
,因为echo
会输出其参数。getLastLineOfLog.sh
与此类似,您已经只有最后一行,因此不需要另一个
tail
。编辑:
事实上,您也可以通过在 sed 中执行以下操作来摆脱尾部:
getLastLineOfLog.sh
getFileName.sh
You don't need the
echo
, as thatecho
outputs its arguments.getLastLineOfLog.sh
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
我知道这是一个非常老的问题,但这里有两种方法可以仅以 bash 方式且不使用管道来获取最后一个文件:
第一种方法:
<前><代码>:* &&回声$_
man bash
说:<块引用>
$_
在扩展后扩展为上一个命令的最后一个参数。因此,您在这里执行命令
:
(不执行任何操作),并将所有文件作为参数,然后使用$_
获取最后一个文件。第二种方法:
通过这种方式,您除了将每个文件分配给
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:
The
man bash
says: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:
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 inlastfile
.