批量脚本命令过滤csv文件中的记录

kqqjbcuj  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(161)

我有一个CSV文件格式如下
Input file
我需要有一个新的文件与以下过滤条件的csv文件
1.过滤掉所有〈=0的度量值
1.如果度量值〉1,则将行复制度量值的次数,并为每个复制的记录将datetime字段的值增加1秒,并将度量值重置为1。
预期结果为output
有人能帮忙吗?
我试过这个

@echo off
setlocal enabledelayedexpansion

set input_file=input.csv
set output_file=output.csv

(for /f "usebackq tokens=*" %%a in ("%input_file%") do (
    set line=%%a
    set measure values=!line:*,=!
    if "!measure values!" gtr "0" (
        echo !line!
    )
)) > "%output_file%"

我尝试了上面的脚本,但它将整个记录复制到输出文件中。
输入文件

Datetime,Measure Names,Category,Measure Values
03/14/2023 16:33:44,Sales ,Table,1
03/14/2023 16:33:44,Amt,Table,0
03/14/2023 16:33:44,Profilt,Table,1
03/14/2023 16:33:44,Qty,Table,2

CSV格式的预期输出

Datetime,Measure Names,Category,Measure Values
03/14/2023 16:33:44,Qty,Table,1
03/14/2023 16:33:45,Qty,Table,1
x8goxv8g

x8goxv8g1#

我不知道一个批处理文件是最好的,但:

@echo off
setlocal enabledelayedexpansion

set input_file=input.csv
set output_file=output.csv

(
    REM Loop through the lines of the input file, splitting each line into tokens using commas as delimiters.
    for /f "usebackq tokens=1-4 delims=," %%a in ("%input_file%") do (
        call :set_variables "%%a" "%%b" "%%c" "%%d"
        
        REM Check if the value is greater than 1.
        if !value! gtr 1 (
            REM Loop from 1 to the value, incrementing by 1 each time.
            for /l %%i in (1, 1, !value!) do (
                set /a seconds=%%i - 1
                REM Call the subroutine to add seconds to the date and time and set the new_datetime variable.
                call :add_seconds_to_datetime !datetime! !seconds!
                REM Output the new row with the updated date and time and a value of 1.
                echo !new_datetime!,!measure!,!category!,1
            )
        )
    )
) > "%output_file%"

exit /b

:set_variables
set "datetime=%~1"
set "measure=%~2"
set "category=%~3"
set "value=%~4"
goto :eof

REM Subroutine to add seconds to a date and time string in MM/DD/YYYY HH:mm:ss format.
:add_seconds_to_datetime
setlocal
set datetime_str=%1
set seconds_to_add=%2

REM Split the date and time string into date and time.
for /f "tokens=1-2 delims= " %%d in ("%datetime_str%") do (
    set date=%%d
    set time=%%e
)

REM Combine the date and time into an ISO 8601 format.
set "datetime_fmt=!date!T!time!"

REM Use PowerShell to parse the date and time, add the specified number of seconds, and output the new date and time in MM/DD/YYYY HH:mm:ss format.
for /f %%t in ('powershell -noprofile -command "&{[datetime]::ParseExact('%datetime_fmt%', 'MM/dd/yyyyTHH:mm:ss', $null).AddSeconds(%seconds_to_add%).ToString('MM/dd/yyyy HH:mm:ss')}"') do (
    set new_datetime=%%t
)

REM Set the new_datetime variable in the parent scope and exit the subroutine.
endlocal & set "new_datetime=%new_datetime%"
exit /b

input.csv:

04/13/2023 16:45:33, Sales, Table,1
04/13/2023 16:45:33, Amt, Table,0
04/13/2023 16:45:33, Profit, Table,1
04/13/2023 16:45:33, Qty, Table,2

相关问题