如何从bat文件获取所需的输出到csv文件?

时间:2014-12-24 14:04:23

标签: batch-file csv cmd output findstr

我必须在MS-Command提示符下写一个.Bat文件,从一个有100个日志文件的文件夹中提取2个特定字符串“Username”和“last login time”,每个日志文件都有这2个东西。必须将输出打印到csv文件报告中以保存在某处。

echo Processing user's last login time. Please wait...
S:
cd log_files

for %G IN (*.txt) do (findstr /i "Username: Log-In" "%G" >> S:\Sourav\GIS_Login_Time.txt)
echo Execution Complete.
echo Output file created in C:\data folder.
pause

输出文件(格式为txt)如下所示:

*** OS Username:              ADIELA
*** GIS Username:             adiela ( Laura Adie - Horizons, Bullion )
*** Log-In Time:              21/07/2014 06:37:20
*** OS Username:              allanj1
*** GIS Username:             allanj ( Jim Allan - Shared access Lomond House )
*** Log-In Time:              17/12/2014 11:44:22
...

请问您如何修改输出(现在转到文本文件)进入csv文件?输出文件必须有3列。 1)OS用户名2)GIS USername 3)登录时间。 如果我能得到一些有价值的建议,我将不胜感激。 提前谢谢你:)

3 个答案:

答案 0 :(得分:2)

我的JREPL.BAT utility的另一个完美问题 - 混合JScript /批处理脚本,它执行正则表达式搜索并替换文本。 JREPL.BAT是纯脚本,可​​以在任何Windows机器上从XP开始本地运行。

假设每个文件都有相同顺序的所有三个条目(虽然不一定是连续的),那么这一个班轮将创建" output.csv"。请注意,我使用了行继续,因此代码更容易阅读。

type *.txt 2>&1 | jrepl ^
  "OS Username:\s*(.*?)$[\s\S]*?GIS Username:\s*(.*?)$[\s\S]*?Log-In Time:\s*(.*?)$" ^
  "'\x22'+$1+'\x22,\x22'+$2+'\x22,\x22'+$3+'\x22'" ^
  /m /jmatch /o output.csv

- 使用示例文本输出 -

"ADIELA","adiela ( Laura Adie - Horizons, Bullion )","21/07/2014 06:37:20"
"allanj1","allanj ( Jim Allan - Shared access Lomond House )","17/12/2014 11:44:22"


如果您真的只想要GPS用户名而没有带括号的信息,那么您就不需要引号,因为您的数据不再包含逗号:

type *.txt 2>&1 | jrepl ^
  "OS Username:\s*(.*?)$[\s\S]*?GIS Username:\s*(.*?)\s[\s\S]*?Log-In Time:\s*(.*?)$" ^
  "$1+','+$2+','+$3" ^
  /m /jmatch /o output.csv

- 输出 -

ADIELA,adiela,21/07/2014 06:37:20
allanj1,allanj,17/12/2014 11:44:22


添加列出列名称的标题行很容易:

type *.txt 2>&1 | jrepl ^
  "OS Username:\s*(.*?)$[\s\S]*?GIS Username:\s*(.*?)\s[\s\S]*?Log-In Time:\s*(.*?)$" ^
  "$1+','+$2+','+$3" ^
  /jbeg "output.WriteLine('OS Username,GIS Username,Login Time')" ^
  /m /jmatch /o output.csv

- 输出 -

OS Username,GIS Username,Login Time
ADIELA,adiela,21/07/2014 06:37:20
allanj1,allanj,17/12/2014 11:44:22

答案 1 :(得分:0)

这是一个很好的。 以下是我解决它的方法:

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion

SET SourceFile="C:\SourceFile.txt"
SET OutputFile="C:\List.txt"

SET TempFile1="%TEMP%\1.txt"
SET TempFile2="%TEMP%\2.txt"
SET TempFile3="%TEMP%\3.txt"

REM Initialize files.
ECHO Header>%TempFile1%
ECHO Header>%TempFile2%
ECHO Header>%TempFile3%

IF EXIST %OutputFile% DEL %OutputFile%

REM Build a list of each value.
FOR /F "usebackq tokens=1,* delims=:" %%A IN (`FINDSTR /L "OS" %SourceFile%`) DO ECHO %%B>>%TempFile1%
FOR /F "usebackq tokens=1,* delims=:" %%A IN (`FINDSTR /L "GIS" %SourceFile%`) DO ECHO %%B>>%TempFile2%
FOR /F "usebackq tokens=1,* delims=:" %%A IN (`FINDSTR /L "Log-In" %SourceFile%`) DO ECHO %%B>>%TempFile3%

REM All temp files should have the same number of entries.
REM Concatenate them with a comma.
SET Skip=1

:ProcessLine
REM Keep going until we find the first empty entry.
REM This means we reached the end of the list.
SET "OSUserName="
SET "GISUserName="
SET "LogInTime="

REM Only set the first line.
FOR /F "usebackq tokens=* skip=%Skip% delims= " %%A IN (%TempFile1%) DO IF "!OSUserName!"=="" SET OSUserName=%%A
FOR /F "usebackq tokens=* skip=%Skip% delims= " %%A IN (%TempFile2%) DO IF "!GISUserName!"=="" SET GISUserName=%%A
FOR /F "usebackq tokens=* skip=%Skip% delims= " %%A IN (%TempFile3%) DO IF "!LogInTime!"=="" SET LogInTime=%%A

ECHO %OSUserName%
REM Check for the end of the list.
IF "%OSUserName%"=="" GOTO Finish

REM Add to the output.
ECHO %OSUserName%,%GISUserName%,%LogInTime%>>%OutputFile%

REM Increment counter and loop.
SET /A Skip=%Skip%+1
GOTO ProcessLine

:Finish
REM Cleanup.
IF EXIST %TempFile1% DEL %TempFile1%
IF EXIST %TempFile2% DEL %TempFile2%
IF EXIST %TempFile3% DEL %TempFile3%

ENDLOCAL

答案 2 :(得分:0)

这直接从源文件构建你的csv(没有临时文件):

@echo off
setlocal enabledelayedexpansion
REM write first line:
echo OSUser;GISUser;LoginTime >out.csv
REM for every .txt file:
for %%i in (*.txt) do (
  REM add the three strings: 
  set "x="
  for /f "tokens=1,* delims=:" %%a in ('findstr "Username Log-In" %%i') do set x=!x!%%b;
  REM delete any "two consecutive spaces":
  set x=!x:  =!
  REM delete TABs ( it's a TAB between : and = ):
  set x=!x:       =!
  REM delete last ; (if it disturbes you):
  set x=!x:~0,-1!
  REM write it to the file:
  echo !x!>>out.csv
)