Page 1 of 1

strip a substring from a messy search result

Posted: 06 Jul 2009 15:13
by jles
I am trying to get a clean output from findstr (other suggestions are welcome).

If I run the comand:

Code: Select all

findstr /n SoftVersion Test.xoml


I get the following output:

Code: Select all

8:                              <ns3:TestAndImproveSharpness x:Name="testAndImpr
oveSharpness1" ImageSharpessWithinThreshold="False" SoftVersion="TestFramew
ork" SettingsFile="{ActivityBind SliceAndViewRecipe,Path=Parameters.ImagingSetti
ngs.SweepAutoFocusConfigurationFileName}" ImageGrabSettings="{ActivityBind Slice
AndViewRecipe,Path=Parameters.AcquireImageParameters.ElectronBeamSemImageSetting
}" Description="Test and Improve Image Sharpness" SharpnessThreshold="{ActivityB
ind SliceAndViewRecipe,Path=Parameters.ImagingSettings.SharpnessThreshold}" />


I would like to some how have a batch file that returns the output like this:

Code: Select all

8: SoftVersion="TestFramework"


I have tried various loops that help separate the line number from the rest of the text but I can not figure out how to separate the specific text (softVersion="SomeValueHere") from the rest of the output.

Any help is appreciated.
Thanks,
Joshua

here is the content of the text.xoml file:

Code: Select all

<ns0:SliceAndViewRecipeBase x:Name="sample" >
   <ns2:ForEachSliceActivity NumberOfSlices="{ActivityBind SliceAndViewRecipe,Path=Parameters.NumberOfSlices}" SliceAndViewVolume="{ActivityBind SliceAndViewRecipe,Path=Parameters.MillingDefinitions.SliceAndViewMillingParameters.SliceAndViewBox}" x:Name="forEachSlice" Description="A generic flow control activity that executes once for each slice." StartSliceIndex="{ActivityBind SliceAndViewRecipe,Path=Parameters.InitialSliceNumber}">
      <ns2:HiddenSequenceActivity x:Name="sequence_ForEachSliceActivity">
         <IfElseBranchActivity x:Name="ifElseBranchActivity6">
            <IfElseBranchActivity.Condition>
               <RuleConditionReference ConditionName="CheckImageSharpness" />
            </IfElseBranchActivity.Condition>
            <ns3:TestAndImproveSharpness x:Name="testAndImproveSharpness1" ImageSharpessWithinThreshold="False" SoftVersion="TestFramework" SettingsFile="{ActivityBind SliceAndViewRecipe,Path=Parameters.ImagingSettings.SweepAutoFocusConfigurationFileName}" ImageGrabSettings="{ActivityBind SliceAndViewRecipe,Path=Parameters.AcquireImageParameters.ElectronBeamSemImageSetting}" Description="Test and Improve Image Sharpness" SharpnessThreshold="{ActivityBind SliceAndViewRecipe,Path=Parameters.ImagingSettings.SharpnessThreshold}" />
            </IfElseBranchActivity>
      </ns2:HiddenSequenceActivity>
   </ns2:ForEachSliceActivity>
</ns0:SliceAndViewRecipeBase>

Posted: 06 Jul 2009 18:33
by ghostmachine4
if you can download Gawk for windows , here's a oneliner

Code: Select all

C:\test>gawk "{ for(i=1;i<=NF;i++){if($i ~ /SoftVersion/){print $i}}}" file.txt
SoftVersion="TestFramework"


or if you prefer, you can use vbscript

Code: Select all

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile= objFSo.OpenTextFile("c:\test\file.txt")
Do Until objFile.AtEndOfStream
   strLine = objFile.ReadLine   
   If InStr(strLine,"SoftVersion")>0 Then
      s= Split(strLine," ")
      For i=LBound(s) To UBound(s)-1
         If InStr(s(i) , "SoftVersion") > 0 Then
            WScript.Echo s(i)
         End If
      Next
   End If    
Loop


save the above as myscript.vbs and on command line

Code: Select all

c:\test> cscript /nologo myscript.vbs
SoftVersion="TestFramework"


Used VBScript

Posted: 07 Jul 2009 12:45
by jles
Thanks ghostmachine4.

I used your vbscript file. I also needed line numbers so I modified it slightly. At the same time I made it a little more flexible for the search text and file to search. The final script is below.

Thanks again,
Joshua

Code: Select all

'Get Args
Set args = WScript.Arguments
num = args.Count

'Check Args
If num < 2 Then
   WScript.Echo "Usage: " & WScript.ScriptName & " <FindText> <FileName>"
   WScript.Quit 1
End If

'Open File
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.OpenTextFile(args.Item(1))

'Loop through lines in file looking for string
LineNumb = 0
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    LineNumb = LineNumb + 1
    If InStr(strLine, args.Item(0)) > 0 Then
        s = Split(strLine, " ")
        For i = LBound(s) To UBound(s) - 1
            If InStr(s(i), args.Item(0)) > 0 Then
                WScript.Echo CStr(LineNumb) & ": " & s(i)
            End If
        Next
    End If
Loop

'Cleanup
Set objFile = Nothing
Set objFso = Nothing

Posted: 08 Jul 2009 10:34
by avery_larry
Nice solutions. That would have been -- let's say frustrating -- in pure dos.