filter out / replace text string with blank text in text doc

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jamesfui
Posts: 50
Joined: 27 Mar 2010 23:00

filter out / replace text string with blank text in text doc

#1 Post by jamesfui » 28 Mar 2010 00:14

Hi Dostips Batch Scripters,

the bat will filter out / Replace All Specific text string as reference from a text doc (ref.txt) with Blank text into the list in (needfilter.txt) BUT will not Erase the Whole Line together in Series with it..

for example "filter out this text string" but keep this text in series with it.
Result: but keep this text in series with it.

i used notepad successfully replace it but for many Different & LONG text strings need to be filter out in a long list, it is quite time consuming... :(

So if possible the bat will get the Reference text string from (ref.txt) and
replace all relevant text string in (needfilter.txt) WITHOUT erase its text in series with it..

Do anyone know how to solve script like above??
Thanks. :D

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: filter out / replace text string with blank text in text

#2 Post by aGerman » 28 Mar 2010 08:07

Could work, but imho not a good idea to make a batch script for those intentions. You have to ascape all signs like ^, &, <, >, | and it will take a long time, because it would work like that:
- for each line in ref.txt call a subroutine
- for each line in needfilter.txt replace all special characters one by one
- after that replace the search-string
- write it to another file
- replace the old file with the new file and start again until all lines of ref.txt are processed

Better you would use a scripting language like VBScript or something else.

Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: filter out / replace text string with blank text in text

#3 Post by ghostmachine4 » 28 Mar 2010 18:47

jamesfui wrote:Hi Dostips Batch Scripters,

the bat will filter out / Replace All Specific text string as reference from a text doc (ref.txt) with Blank text into the list in (needfilter.txt) BUT will not Erase the Whole Line together in Series with it..

for example "filter out this text string" but keep this text in series with it.
Result: but keep this text in series with it.

i used notepad successfully replace it but for many Different & LONG text strings need to be filter out in a long list, it is quite time consuming... :(

So if possible the bat will get the Reference text string from (ref.txt) and
replace all relevant text string in (needfilter.txt) WITHOUT erase its text in series with it..

Do anyone know how to solve script like above??
Thanks. :D



download gawk for windows at: gnuwin32.sourceforge.net/packages/gawk.htm
and use this one liner

Code: Select all


C:\test>more file
filter out this text string but keep this text in series with it.

C:\test>more ref.txt
filter out this text string

C:\test>gawk "FNR==NR{a[$0];next}{for(i in a)gsub(i,\"\")}1" ref.txt file
 but keep this text in series with it.

jamesfui
Posts: 50
Joined: 27 Mar 2010 23:00

Re: filter out / replace text string with blank text in text

#4 Post by jamesfui » 29 Mar 2010 04:56

thanks for the gawk info. will try it out asap..! :wink:
.
.
But hopefully batch could also accomplish this kind of process too;
i will give more details on this..

for example in a (needfilter.txt)
c:\reports\january\submitted\report_jan10.xls
c:\reports\february\not submitted\report_feb10.xls
c:\...etc

need to filter out / replace the "c:\reports\january\submitted\" with Empty text
so it will just gives back only the filename: report_jan10.xls

another text document will be the (ref.txt) where it lists out all the text strings that need to be filter out:
"c:\reports\january\submitted\"
"c:\reports\february\not submitted\"
"c:\...etc..\"

it will took quite a long time if there is many Different Path to filter them all out one by one using the notepad built in replace feature..

So, is it possible to just keep the filename with extension only without the full path of it.? thanks :?

Post Reply