Solved Search and edit txt files

September 21, 2012 at 16:54:00
Specs: Windows 7
Hello... I need to create a batch that reads all .txt files in a directory and swap two lines that contain some predefined words/char.
I know this sound a bit confuse, but here a example:

Original file "Pokemon.txt" contains:
Pikachu
Pichu
Raichu
And I wish that the final file be like this:
Pichu
Pikachu
Raichu

Thanks in advance!


See More: Search and edit txt files

Report •


#1
September 21, 2012 at 20:44:05
sort < myfile > newfile


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

M2


Report •

#2
September 22, 2012 at 12:48:56
Thanks, but sort isn't what I need... I don't want to order all lines in the file, just swap two specific lines.
Ex.: Let's say that a .txt file (with many lines) have a line that contains the word "Europe" and another line that have the word "America". I want to swap these two lines, if the "Europe line" comes after the "America line". These words won't appear more than one time in the file.

Aw... this is so confuse...


Report •

#3
September 23, 2012 at 19:38:20
✔ Best Answer
:: =====  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 •
Related Solutions


Ask Question