Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:17:04+00:00 2026-06-10T11:17:04+00:00

Just wondering if there is any code in batch file that can find a

  • 0

Just wondering if there is any code in batch file that can find a certain text or word in a .txt file.

For Example:

  1. The quick brown fox jump \mark{1} over the lazy dog
  2. The quick \mark{10} brown fox jump over the lazy dog
  3. The quick brown fox jump over the lazy \mark{100} dog
  4. The quick brown fox jump over the lazy dog \mark{1000}
  5. The \mark{1} quick brown fox jump over the lazy dog
  6. The quick brown fox jump over the lazy dog \mark{100}
  7. The quick brown fox \mark{30} jump over the lazy dog

as you can see from the example above, I want to search for the “\mark{Number here}” word and also if there is any possibility that when there is an occurrence of the same word for example the first line and the fifth line, it will only display the “\mark{1}” of the first line and disregard the same word in the fifth line

so the results will be printed in a txt file will be:

  1. \mark{1}
  2. \mark{10}
  3. \mark{100}
  4. \mark{1000}
  5. \mark{30}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:17:05+00:00Added an answer on June 10, 2026 at 11:17 am

    This should be relatively easy if you download a tool like sed for Windows (or perhaps grep for Windows). The Gnu project has both sed and grep for Windows for free.

    It should also be relatively easy using regex capabilities of VBScript, JScript, or powershell.

    But I thought I would take a stab at using native batch. FINDSTR has primitive regex support, but it cannot extract the matching text, so the batch solution is fairly complex.

    The solution below can find multiple marks on one line. It is also able to count the number of appearances for each distinct mark. The SET search and replace is case insensitive, so I was forced to make this entire solution case insensitive.

    The solution can only handle lines of length ~8191 bytes or less.

    The performance should be good even for very large files as long as the number of lines containing marks is relatively small.

    @echo off
    setlocal disableDelayedExpansion
    set "file=test.txt"
    set LF=^
    
    
    ::The two blank lines above are critical to create linefeed - do not remove.
    
    ::Clear any existing \mark variables
    for /f "delims==" %%A in ('2^>nul set \mark{') do set "%%A="
    
    ::Find all lines that contain at least one valid mark and call a routine
    ::to parse out all marks
    for /f eol^=^%LF%%LF%delims^= %%A in (
      'findstr /ri \mark{[0-9][0-9]*} "%file%"'
    ) do (
      set "ln=%%A"
      call :parseMarks
    )
    
    ::Create file containing found marks only
    >marks.txt (
      for /f "delims==" %%A in ('set \mark{') do echo %%A
    )
    
    ::Create file containing found marks with counts
    >markCounts.txt set \mark{
    
    ::Print the results
    echo Here is a list of found marks
    echo -----------------------------
    type marks.txt
    echo(
    echo Here is a list of found marks with the counts
    echo ---------------------------------------------
    type markCounts.txt
    
    exit /b
    
    :parseMarks
    setlocal enableDelayedExpansion
    set "ln2=!ln:*\mark{=!"
    if !ln2! neq !ln! (
      for /f "tokens=1* delims=}" %%B in ("x!ln2!x") do (
        endlocal
        echo(%%B|findstr /xr x[0-9][0-9]* >nul && (
          for /f "delims=x" %%D in ("%%B") do set /a \mark{%%D}+=1
        )
        set "ln=%%C"
      )
      if defined ln goto :parseMarks
    )
    exit /b
    

    Here is the test.txt file that I used. It has a number of problem test cases that make a batch solution difficult.

    The \mark{} quick brown fox jump \mark{1} over the lazy dog
    The quick \mark{10} brown fox jump over the \mark{99a} lazy dog
    The quick \mark{}99} brown fox jump over the lazy \mark{100} dog! \MARK{22}!
    The quick brown fox jump over the lazy dog \mark{1000} \mark{99
    ;The \mark{1} quick brown fox jump over the lazy dog
    The \mark{!!99} quick brown fox jump over the lazy dog \mark{100}
    \mark{22}The quick brown fox \mark{30} jump over the lazy dog
    exclude \mark{100a}
    exclude \mark{}
    include \MARK{22}
    

    And here are my results

    Here is a list of found marks
    -----------------------------
    \mark{1000}
    \mark{100}
    \mark{10}
    \mark{1}
    \mark{22}
    \mark{30}
    
    Here is a list of found marks with the counts
    ---------------------------------------------
    \mark{1000}=1
    \mark{100}=2
    \mark{10}=1
    \mark{1}=2
    \mark{22}=3
    \mark{30}=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if there is any benchmark software that I can download that will
Just wondering is there any way I can check whether the url links to
i just wondering is there any performance issue or anything thing wrong if that
Hey, I'm just wondering if there's any easy function to make text suitable to
I was just wondering if there were any changes in the Drupal 7 code
Im just wondering if there is any code needed in application to utilize the
Just wondering if there's any benefit to using the style: (function() { <code> })();
Just wondering if there is any way to get the NS records in C#.
Just wondering if there are any pitfalls using .net as the extension for a
Was just wondering if there are any built in functions in c++ OR c#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.