-1

I have a program that put out a log file to the /var/log/myfiles/ directory. these files are kept small in size and the file name is incremented by number . at the end of the day I want to cat the last file to a report. I'm drawing a blank on doing this. I can find the file but its not working in a script.

/var/log/myfile/master
/var/log/myfile/master.1
/var/log/myfile/master.2
/var/log/myfile/master.3

closed as unclear what you're asking by Rui F Ribeiro, roaima, Jeff Schaller, Archemar, msp9011 Jan 21 at 13:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • in your example, should we assume that the .3 version is the "last"? – Jeff Schaller Jan 20 at 22:07
  • Are you really using syslog to write these files? And logrotate to cycle them? If so, then master would be more recent than master.3, and so master should be the "last" file. Is that correct? – roaima Jan 20 at 22:37
1

On the assumption that the file you want ("last file") is the one with the highest numeric extension, you could use zsh's powerful globbing capabilities:

zsh -c 'cat /var/log/master.*(oe['REPLY=${REPLY##*.}']n[-1]) > report'

The wildcard/glob expansion is:

  • /var/log/master.* -- match files that start with /var/log/master.
  • ( ... ) -- defines the "glob qualifier"
  • o -- order the results
  • e['REPLY=${REPLY##*.}'] -- the results to sort are given by stripping the leading text through the first period, turning, for example, /var/log/myfile/master.3 into just 3
  • n -- order the results numerically, not alphabetically
  • [-1] -- after sorting the results, pull out only the last element (largest element)
0

Problem solved. I changed the file names to include date and time stamp. Easier to identify and extract the needed data. Master-20-Jan-2019-18:20.log Master-20-Jan-2019-18:45.log Master-20-Jan-2019-19:18.log

M

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