Batch file command to delete all text after certain word

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jaredblank
Posts: 2
Joined: 14 Jun 2017 12:37

Batch file command to delete all text after certain word

#1 Post by jaredblank » 14 Jun 2017 12:45

I am looking for help in the batch file command to delete all text on a single line after a certain word is found.

Example text:

IMAD: 20170614L1B78J1C000441

I would like to delete all of the text after "IMAD:" so that this only remains on the line.

I tried to use this code but it doesn't work:

for /f "tokens=1 delims=IMAD:" %%a in (didigetit.txt) do (echo %%a >>newfilename.txt)

Any help is GREATLY appreciated!! :D

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: Batch file command to delete all text after certain word

#2 Post by Hackoo » 15 Jun 2017 04:14

Hi :)
To be more clear for us :
Just provide an example of the input file and what did you expect as ouput file !
Thanks !

Lachdanan
Posts: 2
Joined: 14 Jun 2017 15:36

Re: Batch file command to delete all text after certain word

#3 Post by Lachdanan » 16 Jun 2017 12:27

Hello there .o/

Do some restrictions such as "I cannot use any exe that doesn't ship with Windows" apply?
If not, I'd just use a win32 port of the sed command (which you can get from here: http://gnuwin32.sourceforge.net/packages/sed.htm ).

You could use it like so:

Code: Select all

type your_input_file | sed s/"^IMAD:.*"/"IMAD:"/ > your_output_file


If you can't use third party executables, it certainly still can be done, but I think you might need to know for sure of a character that will never, ever appear in your input file, to use as a delimiter.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file command to delete all text after certain word

#4 Post by penpen » 16 Jun 2017 18:15

If such restrictions (Lachdanan has mentioned) apply, then you could use the hybrid JScript/batch script JREPL.BAT created by Dave Benham.

Depending on the answer to the question of Hackoo, it might also be possible to use variable repalcement only.


penpen

jaredblank
Posts: 2
Joined: 14 Jun 2017 12:37

Re: Batch file command to delete all text after certain word

#5 Post by jaredblank » 19 Jun 2017 19:12

Sorry for the delay getting back on, was on family vacation last week. Yes to answer your question, I am limited to Windows Server programs only to accomplish this. Ideally I would like to not use that particular line as my column delimiter and instead add both row and column delimiters to my text file. That having been said, the original text is a bank wire detail report with multiple "garbage" lines that I strip down to only the essential lines with specified items I need for my data import into MS SQL. I use the find function for these essentials and output results into a temp txt file. Then I run the jrepl.bat program to strip out empty white spaces and then output the clean results into another temp file. This particular line above I thought I could use as a row delimiter for the SQL import but upon further review of the end result file, now I see that this particular word is not a constant in every group of wire detail results. So, my desired result would be a batch file that I can use where at the end of each line I put a row delimiter and at the end of each group I insert a column delimiter.

Post Reply