strip a substring from a messy search result

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jles
Posts: 2
Joined: 06 Jul 2009 12:01
Location: Portland, OR

strip a substring from a messy search result

#1 Post by jles » 06 Jul 2009 15:13

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>

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#2 Post by ghostmachine4 » 06 Jul 2009 18:33

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"


jles
Posts: 2
Joined: 06 Jul 2009 12:01
Location: Portland, OR

Used VBScript

#3 Post by jles » 07 Jul 2009 12:45

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 08 Jul 2009 10:34

Nice solutions. That would have been -- let's say frustrating -- in pure dos.

Post Reply