tomasz86 Posted July 27, 2012 Author Share Posted July 27, 2012 @YzöwlWell, the request was more general while here I need to deal with specific lines and at the moment I don't know of any better way then the one I used above. Link to comment Share on other sites More sharing options...
dencorso Posted July 27, 2012 Share Posted July 27, 2012 Yes, you do. By using sed (and perhaps unix2dos to correct the line terminators depending on the implementation of sed you choose) almost any modification one can conceieve, however complex, can be performed fast. I put yanklines together to remove dupes fast, in a file, which is very far from the things sed does well. But while you want to reformat individual lines, one by one, no other tool is needed since sed is very powerful and time-proven. sed can be used as a filter, just like FIND.# delete leading whitespace (spaces, tabs) from front of each line# aligns all text flush leftsed 's/^[ \t]*//' # see note on '\t' at end of file<...>USE OF '\t' IN SED SCRIPTS: For clarity in documentation, we have usedthe expression '\t' to indicate a tab character (0x09) in the scripts.However, most versions of sed do not recognize the '\t' abbreviation,so when typing these scripts from the command line, you should pressthe TAB key instead. '\t' is supported as a regular expressionmetacharacter in awk, perl, and HHsed, sedmod, and GNU sed v3.02.80.Reference: sed1liners Link to comment Share on other sites More sharing options...
allen2 Posted July 27, 2012 Share Posted July 27, 2012 For the record autoit has a function to remove white space of any kind (tab included). Link to comment Share on other sites More sharing options...
Yzöwl Posted July 27, 2012 Share Posted July 27, 2012 I need to deal with specific lines and at the moment I don't know of any better way then the one I used above.Depending upon poison characters, and using a cmd file, simply using a CALL may be all you need.FOR /F "DELIMS=" %%# IN (1.INF) DO CALL :SUB %%#GOTO :EOF:SUB>>TEMP.TXT ECHO=%* Link to comment Share on other sites More sharing options...
tomasz86 Posted July 28, 2012 Author Share Posted July 28, 2012 (edited) Depending upon poison characters, and using a cmd file, simply using a CALL may be all you need.FOR /F "DELIMS=" %%# IN (1.INF) DO CALL :SUB %%#GOTO :EOF:SUB>>TEMP.TXT ECHO=%*You're right There are sometimes comments present:SystemRoot.Files=10 ; %windir% (replace if exist)so this seems to work perfectly:FOR /F "DELIMS=;" %%# IN (1.INF) DO CALL :SUB %%#GOTO :EOF:SUB>>TEMP.TXT ECHO=%*Thank you.Edit: Actually it's still not perfect. This gets broken:Source:TSClientDefault.Files=65618,%TSCLIENTDIR%Result:TSClientDefault.Files=65618, Edited July 28, 2012 by tomasz86 Link to comment Share on other sites More sharing options...
tomasz86 Posted September 19, 2012 Author Share Posted September 19, 2012 This is a "minified" version of the silent yanklines script which is created on the fly from commandline:ECHO>yanklines.vbs ^If WScript.Arguments.Count ^<^> 2 thenECHO>>yanklines.vbs WScript.QuitECHO>>yanklines.vbs end ^IfECHO>>yanklines.vbs Const ForReading=1, ForWriting=2ECHO>>yanklines.vbs Dim i,jECHO>>yanklines.vbs Set objFSO=CreateObject("Scripting.FileSystemObject")ECHO>>yanklines.vbs Set objInputFile=objFSO.OpenTextFile(WScript.Arguments.Item(0),ForReading)ECHO>>yanklines.vbs Set objOutputFile=objFSO.OpenTextFile (WorkingDir ^& WScript.Arguments.Item(1),ForWriting,True)ECHO>>yanklines.vbs Set objDict=CreateObject("Scripting.Dictionary")ECHO>>yanklines.vbs objDict.CompareMode=1ECHO>>yanklines.vbs j=0ECHO>>yanklines.vbs On Error Resume NextECHO>>yanklines.vbs While Not objInputFile.AtEndOfStreamECHO>>yanklines.vbs arrinputRecord=split(objInputFile.Readline,"vbNewLine")ECHO>>yanklines.vbs strFirstField=arrinputRecord(0)ECHO>>yanklines.vbs ^If objDict.Exists(strFirstField) thenECHO>>yanklines.vbs j=j+1ECHO>>yanklines.vbs ElseECHO>>yanklines.vbs objDict.add strFirstField,strFirstFieldECHO>>yanklines.vbs End ^IfECHO>>yanklines.vbs WendECHO>>yanklines.vbs colKeys=objDict.KeysECHO>>yanklines.vbs For Each strKey in colKeysECHO>>yanklines.vbs objOutputFile.writeline objDict.Item(strKey)ECHO>>yanklines.vbs NextECHO>>yanklines.vbs objInputFile.CloseECHO>>yanklines.vbs objOutputFile.CloseMaybe someone will fine it useful. Link to comment Share on other sites More sharing options...
jaclaz Posted September 19, 2012 Share Posted September 19, 2012 This is a "minified" version of the silent yanklines script which is created on the fly from commandline:....Maybe someone will fine it useful.IMHO this is - besides smaller - "plainer":1.000 bytes instead of 1.356 BUT including 10 bytes for the "@ECHO OFF", so actually 366 bytes smaller or almost 27% smaller .@ECHO OFF&(ECHO ^If WScript.Arguments.Count ^<^> 2 thenECHO WScript.QuitECHO end ^IfECHO Const ForReading=1, ForWriting=2ECHO Dim i,jECHO Set objFSO=CreateObject^("Scripting.FileSystemObject"^)ECHO Set objInputFile=objFSO.OpenTextFile^(WScript.Arguments.Item^(0^),ForReading^)ECHO Set objOutputFile=objFSO.OpenTextFile ^(WorkingDir ^& WScript.Arguments.Item^(1^),ForWriting,True^)ECHO Set objDict=CreateObject^("Scripting.Dictionary"^)ECHO objDict.CompareMode=1ECHO j=0ECHO On Error Resume NextECHO While Not objInputFile.AtEndOfStreamECHO arrinputRecord=split^(objInputFile.Readline,"vbNewLine"^)ECHO strFirstField=arrinputRecord^(0^)ECHO ^If objDict.Exists^(strFirstField^) thenECHO j=j+1ECHO ElseECHO objDict.add strFirstField,strFirstFieldECHO End ^IfECHO WendECHO colKeys=objDict.KeysECHO For Each strKey in colKeysECHO objOutputFile.writeline objDict.Item^(strKey^)ECHO NextECHO objInputFile.CloseECHO objOutputFile.Close)>yanklines.vbsjaclaz Link to comment Share on other sites More sharing options...
tomasz86 Posted September 19, 2012 Author Share Posted September 19, 2012 Nice I'm wondering if these lines are absolutely necessary:1. If WScript.Arguments.Count <> 2 then WScript.Quit end If 2. objInputFile.CloseobjOutputFile.CloseThe script seems to work without them too... Link to comment Share on other sites More sharing options...
bphlpt Posted September 19, 2012 Share Posted September 19, 2012 (edited) I believe (1) is very necessary, or or least the "< 2" part is, since I'm not sure what will happen, besides error, if you do not specify both the input and output file. If you have extra arguments, ie "> 2", they will just be ignored but it is an indication that something is screwed up in the program call.AFAIK, (2) is not "necessary" since both files "should" be closed when the script exits, but it is best practice and I would always do it. You should always try to clean up after yourself, and not depend on what "should" happen. But that's just my opinion.Cheers and Regards Edited September 20, 2012 by bphlpt Link to comment Share on other sites More sharing options...
dencorso Posted September 20, 2012 Share Posted September 20, 2012 I'm wondering if these lines are absolutely necessary:2. objInputFile.CloseobjOutputFile.CloseThe script seems to work without them too...Removing these lines you'd be introducing a potential bug by relying on behavior which is not guaranteed.And running a large number of times such a buggy script may lead to all kinds of hard to debug conditions.Don't ever forget to close all open files before terminating. Link to comment Share on other sites More sharing options...
jaclaz Posted September 20, 2012 Share Posted September 20, 2012 I believe (1) is very necessary, or or least the "< 2" part is, since I'm not sure what will happen, besides error, if you do not specify both the input and output file. If you have extra arguments, ie "> 2", they will just be ignored but it is an indication that something is screwed up in the program call.I don't get it. That would be a check to make sure that exactly 2 arguments are passed to the script, if the script is invoked only from a batch and only with the correct 2 arguments there is no need for that check (which should be made BEFORE in the calling batch).Or am I missing something?jaclaz Link to comment Share on other sites More sharing options...
tomasz86 Posted September 20, 2012 Author Share Posted September 20, 2012 Removing these lines you'd be introducing a potential bug by relying on behavior which is not guaranteed.And running a large number of times such a buggy script may lead to all kinds of hard to debug conditions.Don't ever forget to close all open files before terminating.What do you mean by "closing" files?This is how I use the script:(ECHO ^If WScript.Arguments.Count ^<^> 2 thenECHO WScript.QuitECHO end ^IfECHO Const ForReading=1, ForWriting=2ECHO Dim i,jECHO Set objFSO=CreateObject^("Scripting.FileSystemObject"^)ECHO Set objInputFile=objFSO.OpenTextFile^(WScript.Arguments.Item^(0^),ForReading^)ECHO Set objOutputFile=objFSO.OpenTextFile ^(WorkingDir ^& WScript.Arguments.Item^(1^),ForWriting,True^)ECHO Set objDict=CreateObject^("Scripting.Dictionary"^)ECHO objDict.CompareMode=1ECHO j=0ECHO On Error Resume NextECHO While Not objInputFile.AtEndOfStreamECHO arrinputRecord=split^(objInputFile.Readline,"vbNewLine"^)ECHO strFirstField=arrinputRecord^(0^)ECHO ^If objDict.Exists^(strFirstField^) thenECHO j=j+1ECHO ElseECHO objDict.add strFirstField,strFirstFieldECHO End ^IfECHO WendECHO colKeys=objDict.KeysECHO For Each strKey in colKeysECHO objOutputFile.writeline objDict.Item^(strKey^)ECHO NextECHO objInputFile.CloseECHO objOutputFile.Close)>TEMP\yanklines.vbsFOR /F "tokens=* delims=" %%I IN ('DIR/B/S "%SPLITDIR%"') DO ( START "yanklines" /WAIT TEMP\yanklines.vbs "%%I" "TEMP\temp.txt" MOVE "TEMP\temp.txt" "%%I") Link to comment Share on other sites More sharing options...
bphlpt Posted September 20, 2012 Share Posted September 20, 2012 (edited) I believe (1) is very necessary, or or least the "< 2" part is, since I'm not sure what will happen, besides error, if you do not specify both the input and output file. If you have extra arguments, ie "> 2", they will just be ignored but it is an indication that something is screwed up in the program call.I don't get it. That would be a check to make sure that exactly 2 arguments are passed to the script, if the script is invoked only from a batch and only with the correct 2 arguments there is no need for that check (which should be made BEFORE in the calling batch).Or am I missing something?jaclazYou are absolutely correct, of course, as long as the assumption is assured. I think both Den and I were looking at the code snippet from a "stand-alone" perspective, or as a function you might paste into another piece of code or share with someone else. No comment was made in the last few posts as to any assumptions or requirements, either before or after yanklines.vbs is invoked, so not knowing if the proper error checking had been done before the function was called ...Removing these lines you'd be introducing a potential bug by relying on behavior which is not guaranteed.And running a large number of times such a buggy script may lead to all kinds of hard to debug conditions.Don't ever forget to close all open files before terminating.What do you mean by "closing" files?He's talking aboutobjInputFile.CloseobjOutputFile.CloseCheers and Regards Edited September 20, 2012 by bphlpt Link to comment Share on other sites More sharing options...
tomasz86 Posted September 20, 2012 Author Share Posted September 20, 2012 objInputFile.CloseobjOutputFile.CloseI know that What I don't know is whether these lines may have any impact when the script is used like I do it. Link to comment Share on other sites More sharing options...
jaclaz Posted September 20, 2012 Share Posted September 20, 2012 Slightly bigger but allows to copy and paste the script "as is" (without any need for carets) at the END of the batch....@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "tokens=1 delims=[]" %%A in ('FIND /N "::THIS IS START OF VBS SCRIPT::" %~dpnx0') DO SET More_offset=%%AECHO 'Hi I am yanklines.vbs script>yanklines.vbsFOR /F "tokens=*" %%A IN ('MORE +%More_offset% %~dpnx0') DO ECHO %%A>>yanklines.vbsECHO do whatever the batch is supposed to doGOTO :EOF::THIS IS START OF VBS SCRIPT::If WScript.Arguments.Count <> 2 thenWScript.Quitend IfConst ForReading=1, ForWriting=2Dim i,jSet objFSO=CreateObject("Scripting.FileSystemObject")Set objInputFile=objFSO.OpenTextFile(WScript.Arguments.Item(0),ForReading)Set objOutputFile=objFSO.OpenTextFile (WorkingDir & WScript.Arguments.Item(1),ForWriting,True)Set objDict=CreateObject("Scripting.Dictionary")objDict.CompareMode=1j=0On Error Resume NextWhile Not objInputFile.AtEndOfStreamarrinputRecord=split(objInputFile.Readline,"vbNewLine")strFirstField=arrinputRecord(0)If objDict.Exists(strFirstField) thenj=j+1ElseobjDict.add strFirstField,strFirstFieldEnd IfWendcolKeys=objDict.KeysFor Each strKey in colKeysobjOutputFile.writeline objDict.Item(strKey)NextobjInputFile.CloseobjOutputFile.Closejaclaz Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now