如何使用新时间戳重写文件中的某些行 [英] How do I rewrite some lines in a file with new timestamps
问题描述
我有一个文件,该文件使用了应用程序的多个不同 API 版本在 API 的 V1 中,字符串中的开始和结束时间戳始终与模式匹配
I have a file that uses several different API versions of an application In V1 of the API there are start and end timestamps in the string that always match the pattern
&startTimestamp=1572580801000&endTimestamp=1572667141000`
所以一个完整的 url 可能看起来像这样(为了安全,省略了一些数据)
so a full url may look something like this (some data omitted for security)
curl -k -X GET "https://serverpathandurl/api/v1/path?query=SELECT%%20xxx%%2C%%20%%20count(xxx)%%20FROM%%20xxx%%20group%%20by%%20xxx&startTimestamp=1572580801000&endTimestamp=1572667141000&explain=false" -H "header " -H "header" > File
我在这个文件中有一些没有时间戳的行.我每天都在同一时间运行这个 CURL 语句文件.所以我知道对于开始和结束时间戳,它总是可以增加一个固定值 86400000
I have some lines in this file that do not have time stamps. I run this file of CURL statements at the same time every day. So I know that it can always incremented by a fixed value of 86400000 for both the start and end time stamp
如何遍历文件,修改具有开始和结束时间戳的新增量值的行,并使用相同的名称写入文件?我认为问题的一部分是更改字符串值并将其作为整数添加,然后重组字符串.
How do I loop through the file, modify the lines that have the new incremented values for start and end time stamps, and write the file with the same name? I assume part of the issue is changing the string value and adding it the value as an intiger, and then reforming the string.
这是一个包含 3 个条目的文件内容示例
Here is an example of the file content with 3 entries
curl -k -X GET "https://serverpathandurl/api/v1/path?query=SELECT%%20xxx%%2C%%20%%20count(xxx)%%20FROM%%20xxx%%20group%%20by%%20xxx&startTimestamp=1572580801000&endTimestamp=1572667141000&explain=false" -H "header " -H "header" > File
curl -k -X GET "https://example2/api/v1/path?query=SELECT%%20xxx%%2C%%20%%20count(xxx)%%20FROM%%20xxx%%20group%%20by%%20xxx&startTimestamp=1572580801000&endTimestamp=1572667141000&explain=false" -H "header " -H "header" > File
curl -k -X GET "https://serverpathandurl/api/v2/metrics/series/xxx?resolution=INF&from=now-1d%%2Fm&scope=entity(xxx)" -H "headerionfo" -H "headerinfo" > file
推荐答案
此答案适用于 PowerShell.
This answer is for PowerShell.
网址中的时间戳是 Unix 毫秒.
The timestamps in the urls are Unix Milliseconds.
要通过每天添加一天 (86400000) 来替换时间戳,您可以执行以下操作:
To replace the timestamps by adding one day (86400000) to them every day, you could do something like this:
$file = 'D:\urls.txt' # your file goes here
$lines = Get-Content -Path $file
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -match 'startTimestamp=(\d+)') {
$newTimeStamp = 'startTimestamp={0}' -f ([int64]$matches[1] + 86400000)
$lines[$i] = $lines[$i] -replace $matches[0], $newTimeStamp
}
if ($lines[$i] -match 'endTimestamp=(\d+)') {
$newTimeStamp = 'endTimestamp={0}' -f ([int64]$matches[1] + 86400000)
$lines[$i] = $lines[$i] -replace $matches[0], $newTimeStamp
}
}
$lines | Set-Content -Path $file -Force
如果您希望能够设置自己的开始和结束时间戳,您可以使用以下转换方法:
If you would like to be able to set your own start and end timestamps, you can play around with the following conversion methods:
要将 Unix MilliSecond 时间戳转换为 datetime 对象:
To convert a Unix MilliSecond timestamp to datetime object:
$utcDate = [DateTimeOffset]::FromUnixTimeMilliseconds(1572667141000).UtcDateTime # gives you the date in UTC
或
$date = [DateTimeOffset]::FromUnixTimeMilliseconds(1572667141000).LocalDateTime # gives you the date in Local time
要将 DateTime 对象转换为 Unix 毫秒时间戳:
To convert a DateTime object into a Unix millisecond timestamp:
# example uses the current date
[DateTime]$origin = [DateTime]::new(1970, 1, 1, 0, 0, 0, 0, 'Utc')
[TimeSpan]$diff = (Get-Date).ToUniversalTime() - $origin
$unixTimeStamp = [int64][Math]::Floor($diff.TotalMilliseconds) # in MilliSeconds
在您的示例中,startTimestamp
转换为 2019-11-01 04:00:01
,endTimestamp
转换为 2019-11-02 03:59:01
(UTC)
startTimestamp 和 endTimestamp 相差 86340000 毫秒(1 天减 1 分钟)
In your example, the startTimestamp
converts to 2019-11-01 04:00:01
and the endTimestamp
converts to 2019-11-02 03:59:01
(Utc)
The difference between the startTimestamp and endTimestamp is 86340000 milliseconds (1 day minus 1 minute)
这篇关于如何使用新时间戳重写文件中的某些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!