0

I have a script that provides the sunrise and sunset times yesterday ($sunrise and $sunset, formatted as yyyy-MM-dd HH:mm:ss). I have a file (could be txt or csv) where the first column has a timestamp in the format of yyyy-MM-dd HH:mm:ss and the second column has a number in the format of XX.X. Using Powershell Get-Content, I am trying to return all of the values that are between the sunrise and sunset times, inclusive. Using Select, I can get singular values, but I can't figure out how to get multiple, let alone a range.

Get-Content C:\uv.txt | select (between $sunrise & $sunset)

Input (uv.txt):

2020-08-11 06:00:00 0.0
2020-08-11 07:00:00 0.0
2020-08-11 08:00:00 0.6
2020-08-11 09:00:00 1.4
2020-08-11 10:00:00 4.2
2020-08-11 11:00:00 6.2
2020-08-11 12:00:00 8.4
2020-08-11 13:00:00 9.3
2020-08-11 14:00:00 9.2
2020-08-11 15:00:00 7.6
2020-08-11 16:00:00 5.6
2020-08-11 17:00:00 3.3
2020-08-11 18:00:00 1.6
2020-08-11 19:00:00 0.5
2020-08-11 20:00:00 0.0
2020-08-11 21:00:00 0.0
2020-08-11 22:00:00 0.0

Simplified script that is not working (no output in console or if I send to out-file):

$sunrise  = "2020-08-12 06:32:17"
$sunset   = "2020-08-12 20:06:33"

Get-Content C:\daily_values_uv.txt | Where-Object { $_ -ge $sunrise -and $_ -le $sunset }
8
  • Understood, @mklement0. Maybe I'll look at a different way of doing this. I create the input file via a mysql query, which I can via batch or powershell. And I've been trying to figure out the easiest way to manipulate the results....doing so within the mysql query or doing so in batch or ps. Commented Aug 12, 2020 at 12:52
  • Given the choice between batch and PowerShell, PowerShell is definitely the way to go: it has vastly superior capabilities. For instance, you could cast your timestamp strings to [datetime] to get System.DateTime instances (e.g., [datetime] '2020-08-10 00:00:00') that you can also compare with -ge and -le and also perform time-span calculations on. Commented Aug 12, 2020 at 13:07
  • If this is in a database, wouldn't it be more efficient to query the database directly rather than extract more data than needed and then script it after the fact? Commented Aug 12, 2020 at 13:43
  • 1
    Your input lines all fall into calendar day 2020-08-11, whereas your comparison variables fall into 2020-08-12 - you shouldn't expect any output. Commented Aug 12, 2020 at 18:25
  • 2
    Oh my...... @mklement0 you're right! And I found the issue. A single missing letter caused the api to pull today instead of yesterday.....I should have caught this a day ago. Now on to adjusting the resulting txt file to remove the timestamps. Thanks for the help! Commented Aug 12, 2020 at 21:31

1 Answer 1

3

Thanks to the big endian date format, you can do:

Get-Content C:\uv.txt | Where-Object { $_ -gt $sunrise -and $_ -lt $sunset }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.