[SOLVED] Batch file command to remove all characters after a certain string inside of a text file

Status
Not open for further replies.

heweaver

Reputable
Dec 4, 2019
37
1
4,545
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!
 
Solution
To my knowledge, Windows commands processor (the one executing BAT files) does not have this functionality. I'd have a look of either PowerShell or external utilities like SED.

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...eference-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

Reputable
Dec 4, 2019
37
1
4,545
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:

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.
 
Status
Not open for further replies.

TRENDING THREADS