Solved Search for substring, remove substring and retain the rest

July 10, 2012 at 01:02:37
Specs: Windows 7
Hi there.

I have no idea how to do this but what I'd like is a batch script that searches a txt file for a certain substring, takes the line that the substring is in, removes everything before that substring (on that line) and and the substring itself before going on to search for the next occurrence of another substring before removing it and everything after it.

For instance if I had the following txt file:

1| asd/random/stuff
2| sdf;sadfgj/name/steve"asdasd
3| asd/name/timm"ads

I'd very much like the script to process all three lines, find /name/, remove it and everything in front of it, then go on to find the next inverted comma (") and remove it and everything after it to leave me with two names: steve and timm.

If anyone can hit me up with how to do this I'd be very very grateful. If this isn't possible in a batch script and there's a quick and easy alternative on windows (no Linux box atm so no bash:( ) I'd appreciate that as well.

Thanks in advance.


See More: Search for substring, remove substring and retain the rest

Report •

✔ Best Answer
July 10, 2012 at 22:15:53
I think you're both half right. M2's code took off the pre but left the post, in my test.
BUT it did leave the target at beginning of the outputline, as you wanted.
These were my attempts, based on M2's core version. version 1:
@echo off > newfile & setLocal enableDELAYedeXpansioN
for /f "tokens=* delims=" %%a in (a2) do (
set S=%%a
set S=!S:*/name/=!
:this is assuming you want to entirely drop other lines... not sure here.
if "!S!" neq "%%a" (
set OS=
call :xquote
>> newfile echo !OS!
)
)
goto :eof

:xquote
set t=%S:~0,1%
if !t! equ ^" goto :eof
set OS=!OS!!t!
set S=%S:~1%
goto :xquote
:end version one

version 2, takes a chance on char. substitution which might foul you up down the road, but shorter code.
@echo off > newfile & setLocal enableDELAYedeXpansioN
for /f "tokens=*" %%a in (a2) do (
set S=%%a
set S=!S:*/name/=!
if "!S!" neq "%%a" (
:swap out quote for at-sign, so the delims can work it. otherwise afaik, it won't.
set S=!S:"=@!
for /f "tokens=1 delims=@" %%b in ("!S!") do set OS=%%b
>> newfile echo !OS!
)
)
:end version two

both these worked on XP using your test data. Not tested on w7.



#1
July 10, 2012 at 03:32:31
::
:: Steveta.bat 2012-07-10 16:57:43.57
@echo off > newfile & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in (my.txt) do (
set S=%%a
set S=!S:*/name/=!
echo.!S!
) >> newfile

goto :eof

::====== script ends here =================


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
July 10, 2012 at 03:42:45
Thanks for taking a crack at it but I just tested this and it seems it actually removes the section that I want (timm or steve) and leaves me with the remainder.

Report •

#3
July 10, 2012 at 04:08:23
I dunno WIN 7 but it works in XP.


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
July 10, 2012 at 22:15:53
✔ Best Answer
I think you're both half right. M2's code took off the pre but left the post, in my test.
BUT it did leave the target at beginning of the outputline, as you wanted.
These were my attempts, based on M2's core version. version 1:
@echo off > newfile & setLocal enableDELAYedeXpansioN
for /f "tokens=* delims=" %%a in (a2) do (
set S=%%a
set S=!S:*/name/=!
:this is assuming you want to entirely drop other lines... not sure here.
if "!S!" neq "%%a" (
set OS=
call :xquote
>> newfile echo !OS!
)
)
goto :eof

:xquote
set t=%S:~0,1%
if !t! equ ^" goto :eof
set OS=!OS!!t!
set S=%S:~1%
goto :xquote
:end version one

version 2, takes a chance on char. substitution which might foul you up down the road, but shorter code.
@echo off > newfile & setLocal enableDELAYedeXpansioN
for /f "tokens=*" %%a in (a2) do (
set S=%%a
set S=!S:*/name/=!
if "!S!" neq "%%a" (
:swap out quote for at-sign, so the delims can work it. otherwise afaik, it won't.
set S=!S:"=@!
for /f "tokens=1 delims=@" %%b in ("!S!") do set OS=%%b
>> newfile echo !OS!
)
)
:end version two

both these worked on XP using your test data. Not tested on w7.


Report •

#5
July 10, 2012 at 22:22:22
Version one worked great! Thank you.

What could I do to remove duplicate results before logging them?


Report •

#6
July 10, 2012 at 22:30:55
Nevermind I found something. For posterity's sake here's what I'll be using to remove duplicate entries. It's a one more bat to run but I don't mind.

@echo off
setlocal enabledelayedexpansion

set inputfile=text1.txt
set outputfile=text2.txt

echo File to be processed
echo.
type %inputfile%
echo.

if exist sorted.txt del sorted.txt
sort %inputfile% /O sorted.txt

if exist %outputfile% del %outputfile%
set lastline=
for /f "delims==" %%L in (sorted.txt) do (
set thisline=%%L
if not "!thisline!"=="!lastline!" echo !thisline!>>%outputfile%
set lastline=%%L
)

del sorted.txt

echo Duplicates removed:
echo.
type %outputfile%


Thanks to those who helped.


Report •

Ask Question