我已经搜索并尝试了此网站发布的各种问题和解决方案。但像我这样的初学者没有脑子。如果我输入 1 个字符串,下面的代码就可以正常工作。如何更改搜索变量以查找匹配值。我希望它像这样工作:查找GXNM 广告服务在第 1 列中并将输出打印到文件中。有人能解释一下吗?
我的 CSV(输入.csv):
Application,RequesterID,Date,VerifierID
"GX AD Service,""9900232"",""10/09/2014 17:51"",""9909142"""
"GX AD Service,""9900249"",""10/09/2014 17:51"",""9909142"""
"GXNT AD Service,""9900232"",""10/09/2014 17:51"",""9909142"""
"GXNT AD Service,""9900249"",""10/09/2014 17:51"",""9909142"""
"GXNM - Others - (Reset Staffs),""99001730"",""11/09/2014 9:00"",""99090615"""
我的批处理程序:
@echo off
cls
setlocal enabledelayedexpansion
set /p header=<input.csv
echo %header%>output2.csv
set column=1
set search=GXNM AD Service
cls
for /f "skip=1 tokens=*" %%A in (output.csv) do (
set inline=%%A
for /f "tokens=1-4 delims=," %%1 in ("!inline!") do (
set col1=%%1&set col2=%%2&set col3=%%3&set col4=%%$
findstr /rx
if !col%column%! equ !search! echo !inline!>>output2.csv
)
)
type output2.csv
set "cmd=findstr /R /N "^^" output2.csv | find /C ":""
for /f %%a in ('!cmd!') do set number=%%a
echo %number%
答案1
这看起来很难处理。你可以用我的命令行工具为此编写的脚本。
@set @junk=1 /*
@echo off
cscript //nologo //E:jscript %0 %*
goto :eof
*/
args = WScript.Arguments
match = args(0);
if(match=="-t") match=args(1).replace("'", "\"");
var stdin = WScript.StdIn;
var stdout = WScript.StdOut;
// stdout.WriteLine("Arguments: "+match);
var regex = new RegExp(match);
while(!stdin.AtEndOfStream) {
var sLine = stdin.ReadLine();
if(regex.test(sLine)) stdout.WriteLine(sLine);
}
我认为这(大部分)是自文档化的,但如果你对此有具体问题,请提出。只需将该文件放在路径中的某个位置,然后像这样运行它:
cat input.csv|jgrep.bat "(AD Service|GXNM)"
这只是使用正则表达式来匹配您的行。该表达式的意思是:
cat input.csv|jgrep.bat - Send the contents of input.csv to the jgrep.bat script.
(AD Service|GXNM) - Print only on lines which contain 'AD Service' or 'GXNM'.
就这么简单。哦,是的,如果您希望有一个标题并将结果保存到文件中,那么它仍然是一行代码:
echo Application,RequesterID,Date,VerifierID>output.txt & cat input.csv|jgrep.bat "(AD Service|GXNM)">>output.txt