中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-06 17:55
48,039 topics / 350,124 posts / today 0 new / 48,252 members
DOS批处理 & 脚本技术(批处理室) » How to use batch to swap two lines in a TXT text?
Printable Version  4,776 / 24
Floor1 lotus516 Posted 2006-11-06 03:07
高级用户 Posts 246 Credits 551
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
Floor2 redtek Posted 2006-11-06 05:48
金牌会员 Posts 1,147 Credits 2,902


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


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 ]
Floor3 namejm Posted 2006-11-06 05:55
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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:


[ Last edited by namejm on 2006-11-6 at 02:03 PM ]
Floor4 redtek Posted 2006-11-06 05:56
金牌会员 Posts 1,147 Credits 2,902
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~
Floor5 namejm Posted 2006-11-06 07:06
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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:
Floor6 vkill Posted 2006-11-06 07:31
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
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
Floor7 lotus516 Posted 2006-11-06 11:59
高级用户 Posts 246 Credits 551
Thanks, but namejm, the command on the 3rd floor didn't work!!!
Floor8 namejm Posted 2006-11-06 12:33
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The code of 3F has many restrictions. If I miss some situations, can you post part of your txt content?
Floor9 lotus516 Posted 2006-11-07 01:48
高级用户 Posts 246 Credits 551

Attachments
1.txt (2.39 KiB)
Floor10 namejm Posted 2006-11-07 01:59
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
Floor11 vkill Posted 2006-11-07 09:07
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
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 ]
Floor12 namejm Posted 2006-11-07 09:23
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The systems in internet cafes are generally cut down. The commands with security issues may all be disabled.
Floor13 vkill Posted 2006-11-07 09:31
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
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 ]
Floor14 namejm Posted 2006-11-07 10:24
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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?
Floor15 无奈何 Posted 2006-11-07 11:09
荣誉版主 Posts 356 Credits 1,338
I also add a sed one, the file should not be too huge.



sed download: http://www.student.northpark.edu/pemente/sed/gsed407x.zip
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023