CoffeeFiend Posted March 13, 2012 Share Posted March 13, 2012 You're missing the out-file at the end (which tells it to write to a file).Then again it could be a one-liner in so many languages... A couple quick examples:in C# (tested), with special thanks to the LINQ's Distinct extension method who does all of the work besides the file I/O (case insensitive -- just lose the StringComparer.CurrentCultureIgnoreCase for a case sensitive version):File.WriteAllLines(args[1], File.ReadAllLines(args[0]).Distinct(StringComparer.CurrentCultureIgnoreCase));or for those who still do VB in 2012 (not tested):File.WriteAllLines(args(1), File.ReadAllLines(args(0)).Distinct(Of String)(StringComparer.CurrentCultureIgnoreCase))Of course I'd add a couple simple guard clauses (args.Length != 2 to show usage, and File.Exist on the source file) and ideally also a try/catch block for the file IO (if this was more than a quickie) but essentially that's all of the relevant code right there. Link to comment Share on other sites More sharing options...
gunsmokingman Posted March 13, 2012 Share Posted March 13, 2012 I did get it to work with this linegc "C:\Users\Gunsmokingman\Desktop\Test1.Txt" | select -unique | out-file "C:\Users\Gunsmokingman\Desktop\New_Test1.Txt"Output File Contents[section 01 Test]Line 01 TestLINE 01 TESTLINE 01 TestLine 01 TESTline 01 testlINE 01 tEST[section 02 Test]Line 02 Test[section 03 Test]Line 03 Test[section 04 Test]Line 04 Test[section 05 Test]Line 05 Test 1Line 05 Test 2[section 06 Test]Line 06 TestFor One Line it works ok, but what changes would be needed to make it case insensitive. Link to comment Share on other sites More sharing options...
CoffeeFiend Posted March 13, 2012 Share Posted March 13, 2012 what changes would be needed to make it case insensitiveMore than you'd want really (I'd be using a hashset[string] instead, so more like 5 lines). That's why when the scope of the project start to fall outside of what powershell does best I switch to other tools. For example, see the C#/VB one-liners (both case insensitive) in the post directly above yours. There are plenty more options out there that don't need 80+ lines for such a simple task. My point isn't so much to solve this particular problem (yanklines already works for him, no point in me wasting time on a already solved problem) but rather that there are so many other options out there. Then again, if I was working on whatever he's doing (I believe merging inf files or somesuch) I'd look at solving the problem as a whole instead of just working on this script. I probably would have if the problem and solution were well defined. Link to comment Share on other sites More sharing options...
jaclaz Posted March 13, 2012 Share Posted March 13, 2012 Yes, I considered that. However, I do think the case-insensitive behaviour is in line with the general behaviour of both DOS and Windows (FIND is an exception, there must be others...) and generally more useful. Now, since to restore the original case sensitive behaviour of the script is just a matter of changing a single "1" to "0" inside the script (and it's documented by a comment at that point), I decided adding a command-line parameter to control it would be an overkill, since it would entail many lines of code for a robust parsing of the command-line, which would add unneeded complexity to the code. That's just my 2 ¢, of course.I see : maybe then, having TWO files:yanklinesCI.vbsyanklinesCS.vbscould be handy. jaclaz Link to comment Share on other sites More sharing options...
dencorso Posted March 13, 2012 Share Posted March 13, 2012 Done. Thanks! Link to comment Share on other sites More sharing options...
gunsmokingman Posted March 13, 2012 Share Posted March 13, 2012 Could you not add something like this to your script, it ask do you want it case sensitive or not case sensitive?This would save having 2 script posted. If MsgBox( _ "Would you like to make the script be Case senistive?" & vbCrLf & _ "Examble Case Insenistive A1b, a1B, A1B are not dublicates" & vbCrLf & _ "and only one entry would be applied" & vbCrLf & _ "Examble Case Senistive A1b, a1B, A1B are dublicates would" & vbCrLf & _ "be added as separate entries" & vbCrLf & _ "Yes to make RemoveLines.vbs case Insenistive" & vbCrLf & _ "No to make RemoveLines.vbs case senistive",4132,"Yes No senistive") = 6 Then '-> Case Insensitive Dic.CompareMode = 1 Else '-> Case Sensitive Dic.CompareMode = 0 End if Link to comment Share on other sites More sharing options...
dencorso Posted March 13, 2012 Share Posted March 13, 2012 Of course. But I think it'd be even more intrusive than adding a command-line switch to control whether it's case sensitive or not. Myself, I see case-sensitivity in yanklines more as a bug than as a feature. But I grant it may be useful in some select cases. All in all, I think the two versions of the script, as proposed by jaclaz, and which I've adopted already, by far the best solution. Link to comment Share on other sites More sharing options...
tomasz86 Posted July 20, 2012 Author Share Posted July 20, 2012 (edited) You need :- to comment the lineRunMeWithCScript()- to comment all lines with wscript.echo (unless you want popup for each line). - to launch the script with wscript.exe instead of cscript if you're launching it from another script.I've finally managed to make the script fully silent. Thanks, allen2 This is what I did.1) I commented the following lines:WScript.Echo "usage: YankLines <input_filename> <output_filename>"WScript.Echo("Record: " & strFirstField)wscript.Echo "Total Records Writen: " & objDict.countwscript.Echo "Total Duplicates found: " & jand then changed:If scriptEngine = "WSCRIPT.EXE" ThentoIf scriptEngine = "CSCRIPT.EXE" ThenAfter doing so there's no visible output anymore.This is a shrunken version of the whole script (all comments removed & case insensitive):RunMeWithCScript()If WScript.Arguments.Count <> 2 then WScript.Quit end If Const ForReading = 1, ForWriting = 2Dim i, j Set 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 = 0 On 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.ClosePublic Sub RunMeWithCScript()Dim scriptEngine, engineFolder, Args, arg, scriptName, argString, scriptCommandscriptEngine = Ucase(Mid(Wscript.FullName,InstrRev(Wscript.FullName,"\")+1))engineFolder = Left(Wscript.FullName,InstrRev(Wscript.FullName,"\"))argString = ""If scriptEngine = "CSCRIPT.EXE" Then Dim Shell : Set Shell = CreateObject("Wscript.Shell")Set Args = Wscript.ArgumentsFor each arg in ArgsIf instr(arg," ") > 0 Then arg = """" & arg & """"argString = argString & " " & ArgNextscriptCommand = "cmd.exe /c " & engineFolder & "cscript.exe //U """ & Wscript.ScriptFullName & """" & argStringShell.Run scriptCommand,,FalseWscript.QuitElseExit SubEnd IfEnd Sub Edited July 20, 2012 by tomasz86 Link to comment Share on other sites More sharing options...
dencorso Posted July 20, 2012 Share Posted July 20, 2012 But... but... but if you wished to have it run with wscript.exe, which is the default mode, you could as well just have deleted the first line, and all lines from "Public Sub RunMeWithCScript()" to the end -- the result would basically be the same, in 20 lines less. Link to comment Share on other sites More sharing options...
tomasz86 Posted July 20, 2012 Author Share Posted July 20, 2012 As I said, I know nothing about VBS scripting You mean this, right?If WScript.Arguments.Count <> 2 then WScript.Quit end If Const ForReading = 1, ForWriting = 2Dim i, j Set 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 = 0 On 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.Close Link to comment Share on other sites More sharing options...
dencorso Posted July 20, 2012 Share Posted July 20, 2012 Sure! Link to comment Share on other sites More sharing options...
tomasz86 Posted July 26, 2012 Author Share Posted July 26, 2012 (edited) Is there any simple way to modify yanklines to treat all lines with spaces/tabs at the beginning/end as equal (regardless of the number of spaces)?I mean sth like this:"abc.txt""abc.txt" <-spaces / tabs"abc.txt" <-spaces / tabsspaces-> "abc.txt" Edited July 26, 2012 by tomasz86 Link to comment Share on other sites More sharing options...
dencorso Posted July 27, 2012 Share Posted July 27, 2012 Not easily, no. This kind of thing is better done with sed. sed is unix, of course, but there are countless ports of sed for Win32. I like the cygwin version, but that needs also cygwin1.dll, so probably something based on mingw may suit you better. And sed is very powerful, so I do think you should invest the time needed to get familiar with it. Link to comment Share on other sites More sharing options...
tomasz86 Posted July 27, 2012 Author Share Posted July 27, 2012 I guess I'll need to study sed later on but at the moment I've managed to get through it using the following method.1) This is a real-life example:AppPatch.Help.Files=10,"Help"<-no spacesAppPatch.Help.Files=10,"Help" <-spacesCom.Files=11,com<-no spacesCom.Files=11,com <-spacesADMT.FIles=65622<-no spacesADMT.FIles=65622 <-spaces2) The cmd script:FOR /F %%I IN ('FINDSTR/V "," 1.inf') DO ECHO>>temp.txt %%IFOR /F tokens^=1-2^ delims^=^", %%I IN ('FINDSTR ^"^"^" 1.inf') DO ECHO>>temp.txt %%I"%%J"FOR /F "tokens=1,2 delims=, " %%I IN ('FINDSTR "," 1.inf ^| FINDSTR/V ^"^"^"') DO ECHO>>temp.txt %%I,%%J3) Result:ADMT.FIles=65622<-no spacesADMT.FIles=65622<-no spacesAppPatch.Help.Files=10,"Help"<-no spacesAppPatch.Help.Files=10,"Help"<-no spacesCom.Files=11,com<-no spacesCom.Files=11,com<-no spaces Link to comment Share on other sites More sharing options...
Yzöwl Posted July 27, 2012 Share Posted July 27, 2012 I've managed to get through it using the following method.FOR /F %%I IN ('FINDSTR/V "," 1.inf') DO ECHO>>temp.txt %%IFOR /F tokens^=1-2^ delims^=^", %%I IN ('FINDSTR ^"^"^" 1.inf') DO ECHO>>temp.txt %%I"%%J"FOR /F "tokens=1,2 delims=, " %%I IN ('FINDSTR "," 1.inf ^| FINDSTR/V ^"^"^"') DO ECHO>>temp.txt %%I,%%JNot only does your 'fix' not show that your request of white space from the beginning of lines has been fulfilled you apear to have been a little carried away with a specific character, ^. 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