Mass Text Replaceer

Omg / OMGITSOLD
January 5, 2009 at 18:13:32
Specs: Windows Vista, 4
Ok, I was looking for some script(Preferably Batch, C++, VB is all ok) that Scans input.txt and replaces all "#" with "s" and all tabs " " with a single space " " Any help will be greatly appreciated

See More: Mass Text Replaceer

Report •


#1
January 5, 2009 at 21:36:47
If you choose to use batch alone it would be quite slow unless a third party utility is used. Sfk is a very useful tool for this and can be used from batch files.

Sfk can take a bit to get used to but it can be well worth it, just note that it's arguments are case sensitive.

I think a line that should do it is:


sfk replace input.txt -text /#/s/ "/ / /" -yes

Just rename sfkXXX.exe to sfk.exe before you start.


There is probably a much more elegant and faster option available in one of the other languages you have chosen............


Report •

#2
January 5, 2009 at 21:50:24
The char in this line after the = is a tab.

set str=!str: = !
=============================================


@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set str=%%a
set str=!str: = !
set str=!str:#=s!
echo !str!>> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#3
January 6, 2009 at 01:25:40
M2,

You script is fine unless the file contains exclamation marks! Delayed expansion causes the problem and the only way I have found to get around this problem is toggle it on and off inside the loop.

All the other mucking around is for empty lines, then lines that start with ].

For all that it is quite slow.


@echo off>newfile&setLocal
for /f "delims=" %%a in ('type myfile^|find /n /v ""') do (
set str="%%a"
SETLOCAL ENABLEDELAYEDEXPANSION
set str=!str: = !
set str=!str:#=s!
call :strip in ter nal
:strip
if "%~1%~2%~3"=="internal" (
if "!str:~0,1!"=="]" (
set str=!str:~1,-1!
goto :eof
) else (
set str=!str:~1!
goto strip
)
)
echo.!str!>>newfile
endlocal
)
endlocal

Ditto about the tabs, with pre tags sometimes they work, as with today, and sometimes they don't, like last week.....


Report •

Related Solutions

#4
January 6, 2009 at 04:13:08
Hi Judago,

Good points.

And I should have said the tab is BEFORE the =.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
January 6, 2009 at 05:56:10

C:\>type test.txt
line 1
comment#
line 3
line with tabs (ascii 83 not 32 which is a space)

C:\>perl -pe "tr/#/s/; tr/\t/ /s" test.txt
line 1
comments
line 3
line with tabs (ascii 83 not 32 which is a space)



Report •

#6
January 6, 2009 at 16:52:52
FM,

I thought tab was ascii 9.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#7
January 6, 2009 at 17:20:37
Oops, my bad. You're right.

Report •


Ask Question