批量删除旧行

问题描述 投票:0回答:1

我有一个txt文件,其中包含了一系列的数据,比如说

   Date      Time          Id    Set
2020/05/04 15:22:28      10005   4512
2020/05/04 15:23:47      10005   4528
2020/05/04 15:23:48      10005   4543
2020/05/04 15:23:58      10005   4555
2020/05/04 15:24:00      10005   4533
2020/05/04 15:24:03      10005   4512
2020/05/04 15:24:05      10005   4590

我需要根据写入时间删除最旧的行,如何在txt文件中得到这个最终结果?

2020/05/04 15:24:05      10005   4590
batch-file dos
1个回答
0
投票

对不起,我的能力有限,我在尝试

    powershell.exe -Command get-content "D:\filelog.txt" | where { [datetime]($_.split(','))[0] -ge (get-date).date.adddays(-30)} 
set-content "D:\Newfilelog.txt"

显然我错了,因为它不工作。


0
投票

按升序排序,读取最后一行数据。

rem /* Sort the data in ascending (alphabetic) order, excluding the first line;
rem    then loop through the sorted lines: */
for /F "delims=" %%L in ('more +1 "input.txt" ^| sort') do (
    rem /* Store the current (non-empty) line; this value overwritten in each iteration,
    rem    so eventually the last line is stored in the variable: */
    set "LAST=%%L"
)
rem // Read the first line (header) of the data:
< "input.txt" (set "FIRST=" & set /P FIRST="")
rem // Return the gathered information (first and last lines):
if defined LAST (
    > "output.txt" (
        echo %FIRST%
        echo %LAST%
    )
)
© www.soinside.com 2019 - 2024. All rights reserved.