Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

Challenge: Strip reserved characters

This challenge is deceptively difficult. I do not currently know a way to complete this task. However, perhaps together we can come up with something that works..

Write a pure batch script that processes a text file called input.txt and outputs a copy of it called output.txt with all instances of the following removed:

1. Double Quote "
2. Single Quote '
3. Caret ^
4. Open Bracket (
5. Close Bracket )
6. And &
7. Greater-Than >
8. Less-Than <
9. Equals =
10. Exclamation !


So for example, consider the following input.txt:
Code
Cats = "soft".  They are 'furry' & nice.
Dum de dum..  <(^_^)> Cats
Best cats!  Other cats.

After running the script, output.txt would contain this:
Code
Cats  soft.  They are furry  nice.
Dum de dum..  _ Cats
Best cats  Other cats.

Ok, go ! :D
(Jul 28, 2015 03:09 PM)annikk.exe Wrote:  Ok, go ! :D

How on earth is this possible? Would you echo the input.exe through a for statement, and then use if/findstr statements to rewrite the document without the characters specified?

I might have to sit this one out and stick to Finding a string between two other strings, which seems way too hardcore imo...
(Jul 28, 2015 03:57 PM)Ventriloquism Wrote:  How on earth is this possible? Would you echo the input.exe through a for statement, and then use if/findstr statements to rewrite the document without the characters specified?

I might have to sit this one out and stick to Finding a string between two other strings, which seems way too hardcore imo...

seems possible but is the line break required?

EDIT: nvm mixed input and output
You can easily replace all the symbols using set "var=%var:!=%" except the "=" for which we need "setlocal enabledelayedexpansion"
(Jul 29, 2015 06:40 AM)eXaByte Wrote:  You can easily replace all the symbols using set "var=%var:!=%" except the "=" for which we need "setlocal enabledelayedexpansion"

what about the new lines?
I got it working. Here's my code:

Code
@echo off
del output.txt
FOR /F "delims=" %%a IN (input.txt) DO (call :subroutine "%%a")
GOTO :eof

:subroutine
set str=%1
set str=%str:'=%
set str=%str:^=%
set str=%str:(=%
set str=%str:)=%
set str=%str:&=%
set str=%str:>=%
set str=%str:<=%
set str=%str:!=%
set str=%str:"=%
for /F "tokens=1* delims==" %%A in ("%str%") do set str=%%A%%B
echo.%str%>>output.txt
GOTO :eof

:eof
(Jul 29, 2015 09:48 AM)RHOPKINS13 Wrote:  I got it working. Here's my code:

Code
@echo off
del output.txt
FOR /F "delims=" %%a IN (input.txt) DO (call :subroutine "%%a")
GOTO :eof

:subroutine
set str=%1
set str=%str:'=%
set str=%str:^=%
set str=%str:(=%
set str=%str:)=%
set str=%str:&=%
set str=%str:>=%
set str=%str:<=%
set str=%str:!=%
set str=%str:"=%
for /F "tokens=1* delims==" %%A in ("%str%") do set str=%%A%%B
echo.%str%>>output.txt
GOTO :eof

:eof

Nice,
You do not need to define :eof btw
Nice job Rhopkins :> Welcome to HF also.

That solution works, but not as a general case. For example, if there are two "equals" symbols on the same line, it will only remove the first one.

I would propose the following adjustment to get it working for all "=" symbols.

Code
@echo off
if exist output.txt del output.txt
FOR /F "delims=" %%a IN (input.txt) DO (call :subroutine "%%a")
GOTO :eof

:subroutine
    set str=%1
    set str=%str:'=%
    set str=%str:^=%
    set str=%str:(=%
    set str=%str:)=%
    set str=%str:&=%
    set str=%str:>=%
    set str=%str:<=%
    set str=%str:!=%
    set str=%str:"=%
    :findMoreEquals
        for /F "tokens=1* delims==" %%A in ("%str%") do set str="%%A%%B"
        set eq=0
        for /f %%a in ('echo %str% ^| Findstr ^=') do (set eq=1)
        set str=%str:~1,-1%
    if %eq% == 1 goto :findMoreEquals
    echo.%str%>>output.txt
goto :eof

Works on my tests. Anyone can confirm ? :>
(Jul 29, 2015 11:44 AM)annikk.exe Wrote:  Nice job Rhopkins :> Welcome to HF also.

That solution works, but not as a general case. For example, if there are two "equals" symbols on the same line, it will only remove the first one.

I would propose the following adjustment to get it working for all "=" symbols.

Code
@echo off
if exist output.txt del output.txt
FOR /F "delims=" %%a IN (input.txt) DO (call :subroutine "%%a")
GOTO :eof

:subroutine
    set str=%1
    set str=%str:'=%
    set str=%str:^=%
    set str=%str:(=%
    set str=%str:)=%
    set str=%str:&=%
    set str=%str:>=%
    set str=%str:<=%
    set str=%str:!=%
    set str=%str:"=%
    :findMoreEquals
        for /F "tokens=1* delims==" %%A in ("%str%") do set str="%%A%%B"
        set eq=0
        for /f %%a in ('echo %str% ^| Findstr ^=') do (set eq=1)
        set str=%str:~1,-1%
    if %eq% == 1 goto :findMoreEquals
    echo.%str%>>output.txt
goto :eof

Works on my tests. Anyone can confirm ? :>

I do believe that the point of this was to do the challange not to do a general solution 👍
Nope, a general solution is the point here. :> What use is the script if it only works on that one example?
(Jul 29, 2015 02:11 PM)annikk.exe Wrote:  Nope, a general solution is the point here. :> What use is the script if it only works on that one example?

The fun of completing the challenge, because we do the challenges for fun .. well at least me :P
I stay in this section generally for fun or to see an interesting script.
Well in this case we are advancing the state of the art.. :> This has been an open problem in some other scripts I've worked on recently. Now that it has been comprehensively resolved (although perhaps it can be further improved or shortened), other scripts can become more reliable and complete. For any kind of parsing data from a web page, this is going to be invaluable.
Agreed, but batch is definitely not the best language to use for coding this. Definitely makes for a fun challenge though.
Long code for batch when doing this in *nix requires one line 😝

PHP Code

<?php 
Teddi
@Teddi# sed "s/[\"^()&><=!']//g" file
Cats  soft.  They are furry  nice.
Dum de dum..  _ Cats
Best cats  Other cats
. 

Just wanted to be the guy who posts a *nix solution in Batch thread.. 😝
This section of the forum and more specifically batch scripts sparked in me the love for coding. I still like batch mostly because its challenging and fun scripting language. But if i need to do a solution for a more complex problem i would use a language that does it much easier as Loki123 showed us.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by p3t3yxxnessJan 21, 2018 01:23 PM 4 156 Jan 21, 2018 01:23 PM
Last Post: VmX5
[Python] by CashDec 8, 2016 06:20 PM 5 170 Dec 8, 2016 06:20 PM
Last Post: Goodies
[C#] by forgotOldAccountCredsOct 1, 2016 10:19 AM 1 297 Oct 1, 2016 10:19 AM
Last Post: Ninja