2

I have a batch script which will print the entire line of search string into a text file.

   for %%i in (configure.txt) do (
   FINDSTR /C:"Widget Name=" %%i >> output.txt
   )

Here is how my Configure.txt file will look like:

   <Widget Name="example_1_new" Type="IFC"/>
   <Property Name="exmaple_2_old"/>
   <Content>
   <Widget Name="example_3_new"/>
   <Widget Name="example_4_old"/>
   <Widget Name="exmaple_1_new"/>
   </Content>

I want to print the next word(example1) after the "Widget Name="(Search String) and also find the duplicates of the word(Example1) and print its count next to it,So that my output should be like:

   example_1_new 2

Can someone let me know how to do this? Please note the configure.txt has around 1000 lines of data like above. I just gave an example.

4
  • So my solution isn't one anymore. If you installed cigwin, You could do quick unix style programs by using awk. Here is very good reference, step by step: grymoire.com/Unix/Awk.html
    – J. Chomel
    Jun 29, 2016 at 7:20
  • I will try awk for my next one , I need to do it in batch now for my above mentioned task. Please let me know your suggestions for the above if you are good at batch scripting @Chomel
    – S6633d
    Jun 29, 2016 at 7:23
  • Sorry I'm not. Good luck here!
    – J. Chomel
    Jun 29, 2016 at 7:24
  • My updated script works however there is an anagram-like typo in your data: example_1 versus exmaple_1
    – JosefZ
    Jun 29, 2016 at 18:53

1 Answer 1

2

Next commented code snippet should do the job:

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

rem delete auxiliary variables
for /F %%G in ('set ___ 2^>NUL') do set "%%G=" 

rem count up number of occurrences 
for /F tokens^=2^ delims^=^" %%G in ('
    FINDSTR /C:"Widget Name=" "configure.txt"
  ') do set /A "___%%G+=1"

rem subtract 1 to get number of duplicates
for /F "tokens=1,2 delims==" %%G in ('set ___ 2^>NUL') do set /A "%%G-=1"

REM output: does dot work if your data contains _ underscore
REM for /F "tokens=1,2 delims=_=" %%G in ('set ___ 2^>NUL') do echo %%G %%H

rem alternative output
for /F "tokens=1,2 delims==" %%G in ('set ___ 2^>NUL') do (
  set "_auxvarname=%%G"
  call set "_auxvarname=%%_auxvarname:~3%%"
  SETLOCAL EnableDelayedExpansion
    echo !_auxvarname! %%H
  ENDLOCAL
)

To create output file, change last lines as follows:

rem alternative output
> output.txt (
    for /F "tokens=1,2 delims==" %%G in ('set ___ 2^>NUL') do (
      set "_auxvarname=%%G"
      call set "_auxvarname=%%_auxvarname:~3%%"
      SETLOCAL EnableDelayedExpansion
        echo !_auxvarname! %%H
      ENDLOCAL
    )
)

Output with data merged from original question and from updated one:

==> type "configure.txt"
<Widget Name="example1"/>
<Property Name="exmaple2"/>
<Content Name="example3"/>
<Widget Name="example4"/>
<Widget Name="example1"/>
<Widget Name="example_1_new" Type="IFC"/>
<Property Name="exmaple_2_old"/>
<Content>
<Widget Name="example_3_new"/>
<Widget Name="example_4_old"/>
<Widget Name="exmaple_1_new"/>
</Content>

==> D:\bat\SU\1094786.bat
example1 1
example4 0
example_1_new 0
example_3_new 0
example_4_old 0
exmaple_1_new 0
6
  • It just prints all the Widget Names, even if it is seen twice it is printed once in the output file. My input file widget name has "example_1" "example_2" etc.. Is the _ affecting the above code output in anyway? @josefZ
    – S6633d
    Jun 29, 2016 at 17:34
  • I have edited my inputs again for your clarity.. Please check it.
    – S6633d
    Jun 29, 2016 at 17:38
  • I am getting output like this : example_1_new example_3_new example_4_old
    – S6633d
    Jun 29, 2016 at 17:39
  • I just need to print the duplicated widget names and the count which says how many times it had occured in the text file. Please help me with this. Thanks in advance
    – S6633d
    Jun 29, 2016 at 17:42
  • Great.... works nice. :) Few things I dont understand in the script, Can you please give me some explanation @Josefz
    – S6633d
    Jun 30, 2016 at 2:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .