1

I have a series of files that contain a date and I need to get the file which has the latest date, and the latest date modification and pipe it to sed. I have tried various approaches and I cannot seem to get one that works.

for example:

find . -iname '*2014-09-20*' -type f -exec ls -lrt {} \; -exec tail -1 {} \; -exec sed -n -e '/some test/,/some other text/p' {} \;

find . -iname '*2014-09-20*' -type f | xargs ls -lrt | tail -1 | sed -n -e '/some text/,/some other text/p'

the log files look something like this

-rw-r--r--. 1 root root  23042 Sep 18 14:21 logfilename_2014-09-19_20140918141909.log
-rw-r--r--. 1 root root   2343 Sep 18 14:22 logfilename_2014-09-20_20140918142142.log
-rw-r--r--. 1 root root   2343 Sep 18 14:23 logfilename_2014-09-21_20140918142245.log
-rw-r--r--. 1 root root   2343 Sep 19 10:11 logfilename_2014-09-20_20140919101031.log
-rw-r--r--. 1 root root   2343 Sep 19 10:12 logfilename_2014-09-21_20140919101122.log
-rw-r--r--. 1 root root   2343 Sep 19 10:13 logfilename_2014-09-22_20140919101218.log

I am expecting the output to be like this:

TEXT 1

line
line
line
line

TEXT 2

which works when I do this:

sed -n -e '/TEXT 1/,/TEXT 2/p' logfilename_2014-09-20_20140919101031.log

but when I pipe the results to sed from aforementioned cmds, I get no results. This is what I've tried so far:

find . -iname '*2014-09-19*' -type f | xargs stat --format '%Y : %y %n' | sort -nr | cut -d: -f2- | head -1 | sed -n -e '/TEXT 1/,/TEXT 2/p'

find . -iname '*2014-09-19*' -printf "%T@\t%p\n" | sort | awk -F"\t" '{print $NF}' | tail -1 | sed -n -e '/TEXT 1/,/TEXT 2/p'
  • Are the dates in the file or in the file name? Do they correspond to the actual time stamps of the files? Could you show your desired output? What do you mean by "date modification"? – terdon Sep 19 '14 at 15:25
  • The expected output is just the lines from my sed command. The log files do have the date and timestamp in them as well. I was trying to sort the files with ls -lrt to sort by modification time. I realize that feeding the find with the ls -lrt goes file by file so it's not actually sorting anything. – Ptrkcon Sep 19 '14 at 16:25
  • I don't know what you mean by your sed command. Please give us an example of the output you want. Anyway, if you don't need to parse the names, just use find alone. – terdon Sep 19 '14 at 16:27
  • the results of sed -n -e '/some text/,/some other text/p' are the expected output. It just happens to be some log lines that are between two strings. – Ptrkcon Sep 19 '14 at 17:46
1

The simplest approach (but which fails if your files contain spaces or newlines or other weird characters) is

ls -ltr '*2014-09-20*' | tail -n 1

The safer way is

find . -printf "%T@\t%p\n" | sort | awk -F"\t" '{print $NF}'
  • if I | tail -1 your response, how can I pipe that to sed? – Ptrkcon Sep 19 '14 at 17:49
  • @Ptrkcon using a pipe (|)? This is why we need to see your expected output. Please edit your question and I can give you the answer you need. – terdon Sep 19 '14 at 17:52
  • I've updated the question with some more information. Thanks for your help – Ptrkcon Sep 19 '14 at 18:33
1

If you need to use the file with the most recent modification time, the simplest approach is to use zsh instead of some other shell and use its glob qualifiers om to sort matches by modification time and [1] to retain only the first match.

echo *2014-09-20*(om[1])

If you want the latest timestamp based on the timestamp part in the file name, then the usual sorting will do. The following function acts on its last argument:

act_on_last_file () {
  eval 'set -- "${'"$#"'}"'
  echo "$1"
}
act_on_last_file *2014-09-20*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.