China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-20 06:20
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to use batch to swap two lines in a TXT text? View 4,655 Replies 24
Original Poster Posted 2006-11-06 03:07 ·  中国 江西 赣州 电信
高级用户
★★
论坛上抢劫的
Credits 551
Posts 246
Joined 2006-09-21 12:35
19-year member
UID 63270
Status Offline
I have a text with the following content:
Zhang San
Li Si
Wang Er Ma Zi
Xiao Bai
Xiao Hei
Xiao Qiang
...... rem: there are N lines in the middle
Xiao Er
Zhao Da
QQ
DOS
WIN98
WINXP
Now I want to achieve swapping WINXP and Zhang San to get the following text: (that is, swap the first line and the last line of the text)
WINXP
Li Si
Wang Er Ma Zi
Xiao Bai
Xiao Hei
Xiao Qiang
...... rem: there are N lines in the middle
Xiao Er
Zhao Da
QQ
DOS
WIN98
Zhang San
Floor 2 Posted 2006-11-06 05:48 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline

@echo %dbg% off
setlocal enabledelayedexpansion
if == goto :error
if exist %4 del /q %4

for /F "delims=" %%. in (%1) do (set /a line+=1 && set "num!line!=%%.")
(set swapTemp=!num%3!) && (set num%3=!num%2!) && (set num%2=!swapTemp!)
for /L %%i in (1,1,!line!) do (echo !num%%i!>>%4)
start %4
goto :eof

:error
echo.
echo Usage: %0.Bat File to process Line to swap Line to be swapped Swapped file name
echo.
echo Example (A): %0.Bat swap.dat 5 10 a.txt
echo Process the swap.dat file, swap the data of line 5 and line 10, and generate a new file: a.txt
echo.



Usage: If no parameters are added, the usage instructions will be prompted.


Usage: Swap.BAT File to process Line to swap Line to be swapped Swapped file name


Example: Swap.BAT swap.dat 5 10 a.txt
It means: Process the swap.dat file, swap the data of line 5 and line 10, and generate a new file: a.txt
The Swap.BAT file is the batch file name for this processing to swap data, and the owner can name it arbitrarily.



Principle: The first wife and the second wife want to exchange their gifts with each other.
So the first wife first temporarily stores her gift with me. After her hand is empty, she takes the second wife's gift.
After the second wife's gift is handed over to the first wife, her hand is empty. At this time, I give the gift that the first wife temporarily stored with me to the second wife.
So they both exchanged gifts ~ : )


If the specified new file to be generated already exists, it will be automatically deleted without prompt.

[ Last edited by redtek on 2006-11-6 at 05:50 AM ]
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 3 Posted 2006-11-06 05:55 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Suppose the test.txt text you need to process has no sensitive characters under CMD (such as <, >, |, etc.), the first line is not empty, and there are no duplicate lines, then the following code can basically meet your needs:

@echo off
:: Idea:
:: Extract the content of the first line and the last line (if the last line is an empty line, then the extracted content is the non-empty last line content),
:: Then write the extracted last line at the first line of the new text, use the findstr statement to filter out the first line and the last line of the original text, and add the extracted first line after the last line.
:: Because there are conditional restrictions when findstr processes the last line, so first generate an empty line after the last line to ensure the accuracy of extraction
copy test.txt test.bak>nul
echo.>>test.txt
set num=0
setlocal enabledelayedexpansion
for /f "delims=" %%i in (test.txt) do (
set str=%%i
set /a num+=1
if !num! equ 1 set var=%%i
)
>test_.txt echo.%str%
findstr /b /e /v /c:"%str%" /c:"%var%" test.txt>>test_.txt
>>test_.txt echo.%var%
move test.bak test.txt
start test_.txt


[ Last edited by namejm on 2006-11-6 at 02:03 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +2 2006-11-07 09:27
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2006-11-06 05:56 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Originally, it should be for the convenience of the original poster's application.
For example: If you want to swap the 3rd line and the 3rd line from the end, then

   Swap.BAT swap.dat 3 -3 a.txt
 or Swap.BAT swap.dat 3 a.txt

It would be more convenient for the original poster if it could be like this. I really can't do it, forgive me~
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 5 Posted 2006-11-06 07:06 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
On the basis of 3F, the following code is obtained, which can handle lines with repeated content. The principle is similar to 3F, but instead of filtering line content, it filters line numbers:

@echo off
:: Note: Because when findstr /n counts the number of lines, the last empty line will be ignored
:: So, if there is an empty line at the end, please ensure that the number of empty lines is greater than 1
findstr /n .* test.txt>tmp1.txt
for /f "tokens=1* delims=:" %%i in (tmp1.txt) do (
if %%i equ 1 set str=%%j
set line=%%i&set var=%%j
)
>tmp2.txt echo %line%:%var%
findstr /b /v /c:"1:" /c:"%line%:" tmp1.txt>>tmp2.txt
echo 1:%str%>>tmp2.txt
echo.>>tmp2.txt
cd.>result.txt
for /f "tokens=1* delims=:" %%i in (tmp2.txt) do >>result.txt echo.%%j
del /q tmp1.txt tmp2.txt
start result.txt
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2006-11-06 07:31 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
sed -n "$p" >temp.txt
sed "1{d;p}" test.txt |sed "${d;p}" >>temp.txt
sed -n "1p" >>temp.txt
Not tested, theoretically feasible
Floor 7 Posted 2006-11-06 11:59 ·  中国 江西 赣州 电信
高级用户
★★
论坛上抢劫的
Credits 551
Posts 246
Joined 2006-09-21 12:35
19-year member
UID 63270
Status Offline
Thanks, but namejm, the command on the 3rd floor didn't work!!!
Floor 8 Posted 2006-11-06 12:33 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The code of 3F has many restrictions. If I miss some situations, can you post part of your txt content?
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 9 Posted 2006-11-07 01:48 ·  中国 江西 赣州 电信
高级用户
★★
论坛上抢劫的
Credits 551
Posts 246
Joined 2006-09-21 12:35
19-year member
UID 63270
Status Offline
Attachments
1.txt (2.39 KiB, Credits to download 1 pts, Downloads: 19)
Floor 10 Posted 2006-11-07 01:59 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
I'm sorry. When I posted the message, I copied the code before modification and didn't post the corrected content, resulting in the code for 3F missing the statements for writing the first and last lines. Now it has been corrected. Please test again.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 11 Posted 2006-11-07 09:07 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
test.bat
@echo off
::%1 is life, swap %2 and %3
::For example, swap line 1 and line 9 in test.txt, use method test.bat test.txt,1,9
::Temporarily cannot change 3,-3 and so on, but you can use sed -n "$=" %1 to see how many lines there are in total~ Then set/a to calculate the actual + lines

if *%1==* (echo No file specified&pause>nul&goto :eof)
if *%2==* (set s=1) else (set s=%2)
if *%3==* (set e=$) else (set e=%3)
if not "%e%"=="$" if %e% lss %s% (set "s=%3"&set "e=%2")

for /f "tokens=*" %%? in ('sed -n "%e%p" %1') do (
sed -e "%s%h" -e "%e%x" %1|sed "%s% c\%%?" |more>temp.txt
)
pause
exit



Test test.txt
`-=\;',./~!@#o^&*()_+{}|:"<>?
ab
AB
12 34
One two three four five six seven eight nine ○
!·¥……—()——:“?》《
http ://abc.abc.abc/

[ Last edited by vkill on 2006-11-7 at 09:55 AM ]
Floor 12 Posted 2006-11-07 09:23 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The systems in internet cafes are generally cut down. The commands with security issues may all be disabled.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 13 Posted 2006-11-07 09:31 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by namejm at 2006-11-7 09:23:
The systems in internet cafes are generally the ones that have been cut down, and commands with security issues may all have been disabled.

Brother namejm, test it. The principle is to first replace 9 with 1, and then copy 1 as the content of the original file 9~

11 F code update

[ Last edited by vkill on 2006-11-7 at 09:58 AM ]
Floor 14 Posted 2006-11-07 10:24 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Actually, the sed program wasn't saved. I know that Brother Wunaikai just posted it recently, but I can't find the download at the moment. Sweat~~ Can you give a download address?
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 15 Posted 2006-11-07 11:09 ·  中国 浙江 宁波 鹏博士宽带
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
I also add a sed one, the file should not be too huge.


sed -re :a -e "N;$!ba;s/(*)\n(.*)\n(.*)/\3\n\2\n\1/"


sed download: http://www.student.northpark.edu/pemente/sed/gsed407x.zip
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Forum Jump: