gpt4 book ai didi

windows - 将日志数据转换为所需格式的 csv 文件

转载 作者:可可西里 更新时间:2023-11-01 10:57:38 25 4
gpt4 key购买 nike


我有一个日志数据:

Name:Mark
City:London
Country:UK

Name:Ben
City:Paris
Country:France

Name:Tom
City:Athens
Country:Greece

我需要生成格式为 CSV 的输出:

Name   City      Country
Mark London UK
Ben Paris France
Tom Athens Greece

我为此创建的批处理是转换为 CSV 的简单批处理。具体如下:

@echo off

cd /d %~dp0
set infilenm=abc.log
set outfilenm=abc.csv
set beforestr=
set afterstr=,

type nul >%outfilenm%

setlocal enabledelayedexpansion

for /f "tokens=1,2,3 delims=" %%A in (%infilenm%) do (
set line=%%A
echo !line:%beforestr%=%afterstr%!>>%outfilenm%
)
endlocal

exit /b

由于我是批处理脚本的新手,任何人都可以帮我解决这个问题!

最佳答案

您的脚本逻辑有误; for /F 一行接一行地读取,所以你必须收集三行数据才能写出一行输出。

这是一个如何完成任务的例子,不使用 for /F但是input redirection ( < ) set /P 读取日志文件:

@echo off
setlocal EnableDelayedExpansion
for /F %%C in ('^< "abc.log" find /C /V ""') do set /A "COUNT=(%%C+1)/2"
set "FIRST=#"
< "abc.log" > "abc.csv" (
for /L %%I in (1,1,%COUNT%) do (
set "LINE1=" & set /P LINE1=""
if defined LINE1 (
set "LINE2=" & set /P LINE2=""
set "LINE3=" & set /P LINE3=""
if defined FIRST (
echo Name,City,Country
set "FIRST="
)
echo(!LINE1:*:=!,!LINE2:*:=!,!LINE3:*:=!
)
)
)
endlocal

这取决于您的日志文件的显示格式,因此它不会验证留给冒号的字符串。


这是一种更灵活的方法,它基于上述方法,但它按名称收集字段值,这些名称保存在预定义的可配置列表(常量 _LIST )中。一个或多个空行完成返回的行。如果在日志文件的当前处理 block 中找不到某个字段名,则其返回的 CSV 字段为空。这是代码:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem // Define constants here:
set "_INPUT=abc.log" & rem // (log file to process)
set "_OUTPUT=abc.csv" & rem // (CSV file to return)
set "_LIST=Name,City,Country" & rem /* (comma-separated list of field names, which must
rem not contain any of the following characters:
rem `:`, `,`, `*`, `?`, `<`, `>`, `!`, `"`, `=`) */
set "_SEPARATOR=," & rem /* (separator character to be used; the default is `,`;
rem the following separator characters are forbidden:
rem `!`, `^`, `&`, `(`, `)`, `<`, `>`, `|`) */
set "_QUOTED=#" & rem // (if not empty, defines to quote the returned items)
set "_HEADER=#" & rem // (if not empty, defines to write a header row)

set "_SEPARATOR=!_SEPARATOR!," & set "_SEPARATOR=!_SEPARATOR:~,1!"
if not defined _QUOTED (set "QUOTE=") else set "QUOTE="^" & rem/^"
for /F "delims==" %%D in ('2^> nul set $ARRAY[') do set "%%D="
for /F %%C in ('^< "abc.log" find /C /V ""') do set /A "COUNT=%%C+1"
< "abc.log" > "abc.csv" (
set "FLAG=" & if defined _HEADER if defined _LIST (
echo(%QUOTE%!_LIST:,=%QUOTE%%_SEPARATOR%%QUOTE%!%QUOTE%
) else echo(%QUOTE%%QUOTE%
for /L %%I in (1,1,%COUNT%) do (
set "LINE=" & set /P LINE=""
if defined LINE (
for /F "delims=: eol=:" %%J in ("!LINE!") do set "$ARRAY[%%J]=!LINE:*:=!"
set "FLAG=#"
) else (
if defined FLAG if defined _LIST (
set "COLL=" & for %%J in ("!_LIST:,=","!") do (
set "COLL=!COLL!%_SEPARATOR%%QUOTE%!$ARRAY[%%~J]!%QUOTE%"
set "$ARRAY[%%~J]="
)
echo(!COLL:~1!
) else echo(%QUOTE%%QUOTE%
set "FLAG="
)
)
)
endlocal
exit /B

此脚本收集某种数组中的列表项 $ARRAY[]其索引是字段名称,因此是日志文件 block 中每一行(第一个)冒号左侧的字符串,其元素值是(第一个)冒号右侧的字符串,并且可能看起来像这样(与关于示例日志数据的第一 block ):

$ARRAY[Name]=Mark
$ARRAY[City]=London
$ARRAY[Country]=UK

关于windows - 将日志数据转换为所需格式的 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51166380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com