welcome to 's blog...


公告

我的分类(专题)

日志更新

最新评论

留言板

链接

搜索


Blog信息




仿*nix系统Grep 命令最终版脚本
aku1 发表于 2008-7-1 8:46:09

网友给我的题目这是用vbscript结合wmi写的,让我熟悉了下参数控制,以前从没注意参数项,谢谢beginner-bj

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\Jimmy>type c:\fso\file.txt
111
abb

222
aab

333
aaa

444
bbb

555
ccc

666
b

最终效果如下

C:\Scripts>cscript grep.vbs c:\fso\file.txt a 2
Microsoft (R) Windows Script Host Version 5.7
版权所有(C) Microsoft Corporation 1996-2001。保留所

abb     aab
aaa

C:\Scripts>cscript grep.vbs c:\fso\file.txt a 3
Microsoft (R) Windows Script Host Version 5.7
版权所有(C) Microsoft Corporation 1996-2001。保留所

abb     aab     aaa


C:\Scripts>cscript grep.vbs c:\fso\file.txt b2
Microsoft (R) Windows Script Host Version 5.7
版权所有(C) Microsoft Corporation 1996-2001。保留所

 

C:\Scripts>cscript grep.vbs c:\fso\file.txt b 2
Microsoft (R) Windows Script Host Version 5.7
版权所有(C) Microsoft Corporation 1996-2001。保留所

abb     aab
bbb     b


C:\Scripts>cscript grep.vbs c:\fso\file.txt b
Microsoft (R) Windows Script Host Version 5.7
版权所有(C) Microsoft Corporation 1996-2001。保留所

111
abb
222
aab
444
bbb
666
b

'strInputFile  = "C:\FSO\file.txt"
'strString  = "b"
'Remainder = 2
Const ForWriting=2
Const ForReading = 1
Dim Args,Count_n,strInputFile,strstring,Remainder,FSO,objfile,strline,stroutput

Set      Args = Wscript.arguments
Count_n       =  Args.Count
if Count_n =1  then
strInputFile  =  Args.Item(0)
else if Count_n =2 then
strInputFile  =  Args.Item(0)
strString     =  Args.Item(1)
else
Count_n =3
strInputFile  =  Args.Item(0)
strString     =  Args.Item(1)
Remainder     =  Args.Item(2)
end if
end if

Select case Count_n  

Case 1

MsgBox "你必须输入二个以上参数"

Case 2

Set FSO=CreateObject("Scripting.FileSystemObject")
Set objFile=FSO.OpenTextFile(strInputFile,ForReading)
Do Until objFile.AtEndOfStream
        
          strline1=objFile.ReadLine
              
          strnewtext=strnewtext&strline1&" "
         
         
loop
         myarray=split(strnewtext,"  ",-1,1)

 For i=0 to Ubound(Myarray)

        strline=myarray(i)& VbCrlf

 

 if InStr(strline,Strstring)>0 then
       
       strOutput=strOutput&strline
       strOutput=Replace(strOutput," ",VbCrlf)

 end if
Next
if InStr(strOutput,Strstring)=0 then
   msgbox "没有你要的字符" & strOutput

end if
objFile.Close

WScript.Echo strOutput

Case 3

Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFile = FSO.OpenTextFile(strInputFile)

num=0
Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        If InStr(strLine,strString) <> 0 Then
                num=num+1
                n=num mod Remainder
                if n=0 then
                        strOutput = strOutput &strLine &VbCrLf
                else
                        strOutput = strOutput &strLine &VbTab
                end if
        End If
Loop
If InStr(strOutput,strString) = 0 Then
  
 msgbox "没有你要的字符" & strOutput
end if

objFile.Close
WScript.Echo strOutput


end select


 


阅读全文 | 回复(0) | 引用通告 | 编辑


发表评论:

    昵称:
    密码: (游客无须输入密码)
    主页:
    标题:



Powered by Oblog.