Board logo

标题: [文件操作] 如何用批处理实现UNIX中的命令grep? [打印本页]

作者: xiaolie    时间: 2009-10-11 19:21     标题: 如何用批处理实现UNIX中的命令grep?

  小弟刚刚批处理入门.有问题像各位高手请教!如何用批处理实现unix中的grep命令啊!以下是grep的-v.-c,-n,-i的实现.可是-l功能不会实现.即在多文件搜索匹配字符时,如何只显示含有匹配字符的文件的文件名啊!帮帮忙,告诉我怎么实现吧~谢谢啊!
@echo off
if "%1"=="" goto forhelp
if "%1"=="--help" goto help
set %var=find
goto findoperation
goto end


:forhelp
::forhelp switch
echo Usage: grep [OPTION]... PATTERN [FILE]...
echo Try `grep --help' for more information.
goto end

:help
::help switch
echo Usage: grep [OPTION]... PATTERN [FILE] ...
echo Search for PATTERN in each FILE or standard input.
echo Example: grep -i 'hello world' menu.h main.c
echo -v  显示所有不包含指定 string 的行。  
echo -c  计算包含指定 string 的行并显示总数。  
echo -n  将文件行号置于每行开头。  
echo -i  指定搜索不区分大小写。  
goto end

:findoperation
if "%1"=="" call %var%&goto end
if "%1"=="-v" set var=%var% /V&shift&goto findoperation
if "%1"=="-n" set var=%var% /N&shift&goto findoperation
if "%1"=="-i" set var=%var% /I&shift&goto findoperation
if "%1"=="-c" set var=%var% /C&shift&goto findoperation
set var=%var% "%1"&shift&goto findoperation
:end
::the end of the .bat
作者: Batcher    时间: 2009-10-11 23:12

findstr /m




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2