Generating a file that is output from a command only if a condition is satisfied

1

Be the command:

top -o %MEM -b > file.txt

I would like in file.txt only the values that are between two time periods, such as:

between April 7, 2017 07h and April 7, 2017 12h. It's possible? This way, I would have a file with just the entries that interest me.

I would like to save in a file only the entries that are between April 7, 2017 07h and April 7, 2017 12h !!!

    
asked by anonymous 08.04.2017 / 00:00

2 answers

2

It's not completely clear what you want ...

Obviously we can not analyze the memory used in the past. Let's start by saving memory usage (with top ) from now until 2 hours. For this he suggested which:

  • set a sampling interval (example 30 seconds)
  • define a number of samples (120 * 60s / 30s = 240)
top -o '%MEM' -d 30 -n 240 -b > file.txt
... analisar_e_limpar file.txt > resumo.txt

( top has many options that allow you to hide, add fields, etc.).

If we want to start logging somewhere in the future, we can use the cron-tab or use the "wait ..." command: sleep ...; top ...

    
08.04.2017 / 12:17
3

I imagine that grep will not solve this case, however you can with shell script take line by line of the contents of the top, from there you create a logic to take the time and compare for example: suppose the line looks like this: drwxrwxrwx victor: victor hh: mm: ss filename this is an example only you could get that line and break in the spaces, then get the 3 element that would be hh: mm: ss and then break in the ':' then you would have separated hour, minute and second, after that you could compare to hour is equal to or greater than 07? or is the time less than or equal to 11? and then put that line in the final file, the logic I'm sure works, however I'm rusty in shell script and I will not be able to provide you the code: /

    
08.04.2017 / 03:33