找到字符串时从 CSV 文件中删除 header(ms dos 命令行)
remove header from CSV file when string is found (ms dos command line)
我需要用dos命令行处理下面的csv,将其保存为bat文件。
该文件有一个可调整大小的 header 我需要删除,并在找到特定字符串后保留其他行
在这种情况下,我只想保留找到字符串 "Date" 之后的行。
以下文件示例:
CSV:
----
Report,Begin Date,End Date,Currency,Change Currency
Financial Report,2016-03-26 00:00:00.000 -0600,2016-03-27 00:00:00.000 -0600,USD,Change Currency
Method,Deposits,Withdrawals,Reversepayouts,Reversedeposits,Net
PAYPAL,200.00,0.00,0.00,0.00,200.00
VISA2,1650.00,0.00,0.00,0.00,1650.00
VISA3,190.00,0.00,0.00,0.00,190.00
DISCOUNT,200.00,0.00,0.00,0.00,200.00
Total:,2240.00,0.00,0.00,0.00,2240.00
Date,Affiliate,Username,Account Id,Method,Type,Amount,Transaction Id,Note
2016-03-26 00:36:01.746 -0600,JamesX,ad123,30153,VISA2,Deposit,32.0000,244258410,VISA2
2016-03-26 01:25:53.680 -0600,JamesX,ad123,30153,VISA2,Deposit,32.0000,244263044,VISA2
2016-03-26 02:26:05.776 -0600,ChristineY,Sar,30887,ARESYS,Deposit,200.0000,244267597,PAYPAL
2016-03-26 03:53:28.313 -0600,ChristineY,doo15,35088,VISA2,Deposit,100.0000,244271237,VISA2
2016-03-26 05:01:14.420 -0600,ChristineY,doo15,35088,VISA2,Deposit,320.0000,244273790,VISA2
2016-03-26 08:40:38.593 -0600,JamesX,ad123,30153,VISA2,Deposit,33.0000,244290455,VISA2
2016-03-26 10:08:43.230 -0600,xAZER,veso,36504,VISA3,Deposit,90.0000,244302244,VISA3
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q36240256.csv"
SET "outfile=%destdir%\outfile.csv"
SET "reproduce="
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
IF NOT DEFINED reproduce (
ECHO(%%a|FINDSTR /b /L /c:"Date," >NUL
IF NOT ERRORLEVEL 1 SET "reproduce=y"
)
IF DEFINED reproduce ECHO(%%a
)
)>"%outfile%"
GOTO :EOF
您需要更改 sourcedir
和 destdir
的设置以适合您的情况。
我使用了一个名为 q36240256.csv
的文件,其中包含您的数据用于我的测试。
生成定义为 %outfile%
的文件
将 reproduce
标志设置为 nothing(因此未定义)
读取每个文件行。如果 reproduce
未定义,则使用 findstr
查看行 /b
是否以 /L
文字 /c:
常量字符串 "Date," 开头,处理任何输出。
如果 findstr
产生的错误级别不是 >1(即 0),则将 reproduce
设置为某个值。
如果设置了 reproduce
,则反省线路,不要再担心速度慢 findstr
会造成更多盲目 vitesse...
我会使用 a regular expression find/replace utility called JREPL.BAT。 JREPL.BAT 是纯脚本(混合 JScript/batch),从 XP 开始可以在任何 Windows 机器上本地运行。
解决方案是一个基本的正则表达式 find/replace 和一些用户提供的 JScript 来处理丢弃哪些行的逻辑。
如果要丢弃 "Date,..." header 行,则:
jrepl "^(Date,)?.*" "(?i++:i)?[=10=]:false" /jmatch /jbeg "var i=0" /f test.txt /o output.txt
如果要保留 header 行,只需稍作改动:
jrepl "^(Date,)?.*" "(?++i:i)?[=11=]:false" /jmatch /jbeg "var i=0" /f test.txt /o output.txt
如果要用结果覆盖原始文件,请使用/o -
。
如果将命令放在批处理脚本中,请使用 call jrepl
。
没有用户提供的JScript也可以解决;但这需要 /m
(多行)开关,它将整个文件加载到内存中,因此最大文件大小限制在 1GB 左右。
丢弃 header 行:
jrepl "[\S\s]*?^Date,.*\n?([\S\s]*)" "" /m /f test.txt /o output.txt
保留 header 行:
jrepl "[\S\s]*?(^Date,[\S\s]*)" "" /m /f test.txt /o output.txt
我需要用dos命令行处理下面的csv,将其保存为bat文件。 该文件有一个可调整大小的 header 我需要删除,并在找到特定字符串后保留其他行 在这种情况下,我只想保留找到字符串 "Date" 之后的行。 以下文件示例:
CSV:
----
Report,Begin Date,End Date,Currency,Change Currency
Financial Report,2016-03-26 00:00:00.000 -0600,2016-03-27 00:00:00.000 -0600,USD,Change Currency
Method,Deposits,Withdrawals,Reversepayouts,Reversedeposits,Net
PAYPAL,200.00,0.00,0.00,0.00,200.00
VISA2,1650.00,0.00,0.00,0.00,1650.00
VISA3,190.00,0.00,0.00,0.00,190.00
DISCOUNT,200.00,0.00,0.00,0.00,200.00
Total:,2240.00,0.00,0.00,0.00,2240.00
Date,Affiliate,Username,Account Id,Method,Type,Amount,Transaction Id,Note
2016-03-26 00:36:01.746 -0600,JamesX,ad123,30153,VISA2,Deposit,32.0000,244258410,VISA2
2016-03-26 01:25:53.680 -0600,JamesX,ad123,30153,VISA2,Deposit,32.0000,244263044,VISA2
2016-03-26 02:26:05.776 -0600,ChristineY,Sar,30887,ARESYS,Deposit,200.0000,244267597,PAYPAL
2016-03-26 03:53:28.313 -0600,ChristineY,doo15,35088,VISA2,Deposit,100.0000,244271237,VISA2
2016-03-26 05:01:14.420 -0600,ChristineY,doo15,35088,VISA2,Deposit,320.0000,244273790,VISA2
2016-03-26 08:40:38.593 -0600,JamesX,ad123,30153,VISA2,Deposit,33.0000,244290455,VISA2
2016-03-26 10:08:43.230 -0600,xAZER,veso,36504,VISA3,Deposit,90.0000,244302244,VISA3
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q36240256.csv"
SET "outfile=%destdir%\outfile.csv"
SET "reproduce="
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
IF NOT DEFINED reproduce (
ECHO(%%a|FINDSTR /b /L /c:"Date," >NUL
IF NOT ERRORLEVEL 1 SET "reproduce=y"
)
IF DEFINED reproduce ECHO(%%a
)
)>"%outfile%"
GOTO :EOF
您需要更改 sourcedir
和 destdir
的设置以适合您的情况。
我使用了一个名为 q36240256.csv
的文件,其中包含您的数据用于我的测试。
生成定义为 %outfile%
的文件将 reproduce
标志设置为 nothing(因此未定义)
读取每个文件行。如果 reproduce
未定义,则使用 findstr
查看行 /b
是否以 /L
文字 /c:
常量字符串 "Date," 开头,处理任何输出。
如果 findstr
产生的错误级别不是 >1(即 0),则将 reproduce
设置为某个值。
如果设置了 reproduce
,则反省线路,不要再担心速度慢 findstr
会造成更多盲目 vitesse...
我会使用 a regular expression find/replace utility called JREPL.BAT。 JREPL.BAT 是纯脚本(混合 JScript/batch),从 XP 开始可以在任何 Windows 机器上本地运行。
解决方案是一个基本的正则表达式 find/replace 和一些用户提供的 JScript 来处理丢弃哪些行的逻辑。
如果要丢弃 "Date,..." header 行,则:
jrepl "^(Date,)?.*" "(?i++:i)?[=10=]:false" /jmatch /jbeg "var i=0" /f test.txt /o output.txt
如果要保留 header 行,只需稍作改动:
jrepl "^(Date,)?.*" "(?++i:i)?[=11=]:false" /jmatch /jbeg "var i=0" /f test.txt /o output.txt
如果要用结果覆盖原始文件,请使用/o -
。
如果将命令放在批处理脚本中,请使用 call jrepl
。
没有用户提供的JScript也可以解决;但这需要 /m
(多行)开关,它将整个文件加载到内存中,因此最大文件大小限制在 1GB 左右。
丢弃 header 行:
jrepl "[\S\s]*?^Date,.*\n?([\S\s]*)" "" /m /f test.txt /o output.txt
保留 header 行:
jrepl "[\S\s]*?(^Date,[\S\s]*)" "" /m /f test.txt /o output.txt