Question Batch file command to remove all characters after a certain string inside of a text file

heweaver

Prominent
Dec 4, 2019
23
0
510
0
So I have a text file which I have created using a combination of downloading a URL from a website with wget and stripping out the first few lines... so basically my text file contents look like this:
4523">blah blah blah and a whole bunch of useless lines of code.
I simply need (using only windows batch file commands) to create a new text file that ONLY contains whatever the number is before the first "> ... so my new text file will only be:
4523
I need to remove the "> and everything that appears after that string.

I have been having trouble with this, dunno why. It really should be within my skill level I thought... I have googled things and tried things until I am blue in the face lol. Surely someone has a super simple solution for me.
Thanks!
 

Ralston18

Titan
Moderator
The term or process is known as parsing.

Strings. String manipulation.

Start here:

https://www.dostips.com/DtTipsStringManipulation.php

And I would be remiss if I did not ask why you are apparently limited to windows batch files commands. What is constraint?

Python, Powershell, and Visual Basic can do such things. As can other languages.

Excel can do such things.

https://support.microsoft.com/en-us/office/text-functions-reference-cccd86ad-547d-4ea9-a065-7bb697c2a56e

If the initial string is very consistent then the task becomes easier.

Look through the string until > is found then discard it and everything else to the right.

Probably multiple ways available.
 
Last edited:

heweaver

Prominent
Dec 4, 2019
23
0
510
0
Well, it's because I'm launching a batch file from inside of another program... there's a few things going on with the file to get it set with the data we want. the end goal is for me to end up with a single text file with just that one number, and yes it looks like every single text file will be the same, and like you said, could discard the first > and everything after it. I'm more familiar with batch files, and I am trying to eliminate the need for an extra program of any kind. Thanks!

I just know that the initial number could be a 1,2,3, or 4 digit number, or it could be a 0...either way, I need ONLY the data that would be before the ">
 
Last edited:

heweaver

Prominent
Dec 4, 2019
23
0
510
0
Actually, if there's a simple solution involving a third party command line app that can still be triggered from a batch file, that could work too...
 

Ralston18

Titan
Moderator
Agree with @Alabalcho

I favor Powershell.

FYI:

https://philerb.com/2011/11/25/find-text-in-a-file-with-powershell/

If you run the cmdlet but use ">" instead of "ERROR" targeting the .txt file you have does that initial cmdlet find the applicable lines that you need?

That is the first step: finding the lines.

The second step will be to strip away what you do not need from those lines.

If you can post a few representative lines of your text file without revealing any personally identifying or sensitive information then do so.

Seeing exactly what you are working with may help someone offer a solution or suggestion.
 

ASK THE COMMUNITY

TRENDING THREADS