Solved Find and Move Text

September 22, 2012 at 17:00:23
Specs: Windows XP
Hi all,
Please your advice. I'm working with text file.
I need a batch script to find "pindah" word and move it and word after to next lines.
for instance :
before :
Tidak selamanya kita tinggal di rumah yang sama namun ada kalanya kita harus pindah ke rumah lain untuk memberi ruang yang lebih besar bagi keluarga kita.
after script
Tidak selamanya kita tinggal di rumah yang sama namun ada kalanya kita harus
pindah ke rumah lain untuk memberi ruang yang lebih besar bagi keluarga kita.

after we run the script, it will move "pindah"and all sentences to next line.
Please your advice and help
Thanks a lot
Alex:


See More: Find and Move Text

Report •


#1
September 23, 2012 at 06:05:55
:: =====  script starts here  ===============
:: lesson learned: AGAIN space is a char
:: swapline.bat  2012-09-23 17:40:13.10
@echo off > newfile & setLocal enableDELAYedeXpansioN
if %2'==' echo which two words? && goto :eof

for /f "tokens=* delims= " %%a in ('find /i "%1" ^< myfile') do ( set A=%%a)
for /f "tokens=* delims= " %%a in ('find /i "%2" ^< myfile') do ( set B=%%a)

for /f "tokens=* delims= " %%a in (myfile) do (
if not "%%a"=="!A!" if not "%%a"=="!B!" echo %%a
if "%%a" equ "!B!" echo.!A!
if "%%a" equ "!A!" echo.!B!
)  >> newfile
::======  script ends here  =================


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

M2


Report •

#2
September 23, 2012 at 08:39:45
Hi M2,
my file is source.txt : Tidak selamanya kita tinggal di rumah yang sama,ada kalanya kita harus pindah ke rumah lain untuk memberi ruang yang lebih besar bagi keluarga kita

script :
:: lesson learned: AGAIN space is a char
:: swapline.bat 2012-09-23 17:40:13.10
@echo off > newfile & setLocal enableDELAYedeXpansioN
if %2'==' echo which two words? && goto :eof

for /f "tokens=* delims= " %%a in ('find /i "%1" ^< source.txt') do ( set A=%%a)
for /f "tokens=* delims= " %%a in ('find /i "%2" ^< source.txt') do ( set B=%%a)

for /f "tokens=* delims= " %%a in (source.txt) do (
if not "%%a"=="!A!" if not "%%a"=="!B!" echo %%a
if "%%a" equ "!B!" echo.!A!
if "%%a" equ "!A!" echo.!B!
) >> newfile

Then I run the script, it will create a newfile file but empty.
Please advice.
Many thanks


Report •

#3
September 23, 2012 at 10:29:24
✔ Best Answer
(@M2go, correct me if i'm wrong!)
judging from the code (excellent, i might add), it looks like M2 got wires crossed with this post:
http://www.computing.net/answers/pr...

if so, then here is something to try for this objective:

::==== begin
@echo off & setlocal enabledelayedexpansion
::---- this is the output file, "source.out":
set output=source.out
set word=pindah>%output%
for /f "tokens=*" %%a in (source.txt) do (
set xx=%%a
call :data
)
goto :eof

:data
set test=!xx:%word%=#!
if "%test%" neq "%xx%" (
for /f "tokens=1* delims=#" %%b in ("%test%") do (
>>%output% echo %%b
>>%output% echo %word%%%c
)
) else (
>>%output% echo %xx%
)
::===== end


Report •

Related Solutions


Ask Question