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 7714025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:01:12+00:00 2026-06-01T02:01:12+00:00

I have to filter a text file filter.tmp containing two types of lines, this

  • 0

I have to filter a text file filter.tmp containing two types of lines, this shows the difference:

findstr /r "^[0-9][0-9]*.*$" filter.tmp > filter-numbers.tmp
findstr /r "^[^0-9][^0-9]*.*$" filter.tmp > filter-text.tmp

What I need to do is to append lines containing text together like this and if line does contain number just put it to output file:

IF "current line" contains text THEN
previous line = concatenate "previous line" + "/" + "current line"
ELSE
echo "previous line" >> filter.out
echo "current line" >> filter.out

filter.tmp contains something like:

Hello
World
Foo
Bar
45: this is some line
Trouble
with code
66: another line

filter.out should look like:

Hello/World/Foo/Bar
45: this is some line
Trouble/with code
66: another line

I realize, this is very simple, but I just can not get it working. As I am thinking about it, it would be much easier to use C++….

  • 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-01T02:01:14+00:00Added an answer on June 1, 2026 at 2:01 am

    This is a quite verbatim translation of your pseudocode and your regexes, based on the assumption that »contains numbers« really means »starts with two digits« (which is what your regexes show):

    @echo off
    setlocal enabledelayedexpansion
    set Prev=
    for /f "delims=" %%x in (filter.tmp) do (
        set "Line=%%x"
        if "!Line:~0,2!" GEQ "00" if "!Line:~0,2!" LEQ "99" (
            if not "!Prev!"=="" (>>filter.out echo !Prev!)
            >>filter.out echo !Line!
            set Prev=
        ) else (
            if "!Prev!"=="" (set "Prev=!Line!") else (set "Prev=!Prev!/!Line!")
        )
    )
    if not "!Prev!"=="" (>>filter.out echo !Prev!)
    

    This uses several things. First of all, we need delayed expansion which enables us to manipulate environment variables within the loop. Then we iterate over the lines in the file with for /f. Note that this will skip empty lines in the file, but you cannot avoid that. Inside the for /f loop the variable Line holds the current line and Prev the previous one (if there has been a previous one). I swapped the then and else branches of the condition since numbers at the start of the line are easier to check for than non-numbers.

    With the echo you’ll notice that I moved the redirection to the start of the line; this is to prevent trailing numbers in Prev or Line from having an effect on the redirection (and also to avoid trailing spaces).

    If you’re not adverse to PowerShell, you can use the following:

    $(switch -Regex -File filter.tmp {
        '^\D'    { if ($prev) { $prev += "/$_" } else { $prev = $_ } }
        '^\d{2}' { if ($pref) {$prev}; $_; $prev = '' }
    }
    if ($prev) { $prev }
    ) | Set-Content filter.out
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which I want to filter using awk. The text
I have some text i need to filter out a list of bad words
I have a text file with two non-ascii bytes (0xFF and 0xFE): ??58832520.3,ABC 348384,DEF
I want to filter the data from a text file in unix. I have
1) I have a text file below like this. Dilantha code 65 po Bo
i have been working on this text extraction project of various file extensions, but
I have a query filter written in human readable language. I need to parse
I have a list filter = ['a', 'b', 'c']. I need to frame the
I'm processing a text file output by a scientific instrument. I do not have
Hi all i have written a small code to open a particular text file.

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.