开发者

SED, Getting the last file name

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 y开发者_Python百科ou in advance.


if you want to sort by the filename:

ls *.log | sort -t, -k2 -n | tail -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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜