|
lotus516
高级用户
    论坛上抢劫的
积分 551
发帖 246
注册 2006-9-21
状态 离线
|
『楼 主』:
如何用批实现TXT文本的某两行互换?
使用 LLM 解释/回答一下
我有一个文本,里面内容如下:
张三
李四
王二麻子
小白
小黑
小强
...... rem:中间为N行
小二
赵大
QQ
DOS
WIN98
WINXP
现在我想实现把WINXP和张三互换成如下文本:(即文本第一行和最后一行互换)
WINXP
李四
王二麻子
小白
小黑
小强
...... rem:中间为N行
小二
赵大
QQ
DOS
WIN98
张三
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
|
|
2006-11-6 03:07 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
只按楼主给出的原始模型处理,
以下代码只考虑到楼主上面非真实模型上的要求,
不考虑楼主没有给出的特殊情况或特殊符号等问题~:)
@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 使用说明:%0.Bat 要处理的文件名 交换行数 被交换行数 交换后文件名
echo.
echo 例(A):%0.Bat swap.dat 5 10 a.txt
echo 处理swap.dat文件,第5行与第10行数据交换,生成新文件:a.txt
echo.
使用说明: 如果未加参数则提示使用说明信息。
使用说明: Swap.BAT 要处理的文件名 交换行数 被交换行数 交换后文件名
例: Swap.BAT swap.dat 5 10 a.txt
表示: 处理 swap.dat 文件,第5行与第10行数据交换,生成新文件:a.txt
Swap.BAT 文件为这个处理交换数据的批处理文件名,楼主可以任意起。
原理: 大老婆和二老婆想互相交换她们手中的礼物,
于是大老婆先把礼物暂存在我这里,手腾空了后把二老婆的礼物拿了过去。
二老婆的礼物交给了大老婆以后,手腾空了。这时我再把大老婆暂存在我手中的礼物交给二老婆。
于是她们双方都互换了礼物~:)
[备注:] 指定生成的新文件如果是已存在的文件,它将自动无提示删除。
Last edited by redtek on 2006-11-6 at 05:50 AM ]
@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,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-6 05:48 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
假设你要处理的test.txt文本中没有CMD下的敏感字符(如<、>、|等),首行不为空,并且没有内容重复的行,那么,用下面的代码基本能满足你的需求:
@echo off
:: 思路:
:: 提取第一行和最后一行内容(若最后一行是空行,则提取到的是非空的最后一行内容),
:: 然后在新文本的首行写入提取到的最后一行,用 findstr 语句过滤掉原文本的第一行和最后一行,在最后一行
:: 之后添加提取到的第一行。
:: 因为 findstr 处理最后一行有条件限制,所以先在最后一行之后生成一个空行,以保证提取的准确性
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 ]
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 ]
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-6 05:55 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
本来应该为方便楼主应用,
例如:想让第3行与倒数第3行交换,则
Swap.BAT swap.dat 3 -3 a.txt
或 Swap.BAT swap.dat 3 a.txt
如果能这样会更方便楼主,实在是力不从心,谅~~
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,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-6 05:56 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
在3F的基础上得到如下代码,可以处理内容有重复的行。原理和3F近似,只是过滤的不再是行内容,而是过滤行号:
@echo off
:: 注意:因为 findstr /n 统计行数的时候,会忽略最后一个空行
:: 所以,若在最后有空行的话,请保证空行的个数大于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
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-6 07:06 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
sed -n "$p" >temp.txt
sed "1{d;p}" test.txt |sed "${d;p}" >>temp.txt
sed -n "1p" >>temp.txt
未测试,原理上可行
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
|
|
2006-11-6 07:31 |
|
|
lotus516
高级用户
    论坛上抢劫的
积分 551
发帖 246
注册 2006-9-21
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
谢谢,不过namejm,3楼的命令不成功!!!
Thanks, but namejm, the command on the 3rd floor didn't work!!!
|
|
2006-11-6 11:59 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
3F的代码有诸多限制,如果我说漏了某些情况,能否把你的txt内容贴一部分出来?
The code of 3F has many restrictions. If I miss some situations, can you post part of your txt content?
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-6 12:33 |
|
|
lotus516
高级用户
    论坛上抢劫的
积分 551
发帖 246
注册 2006-9-21
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
好,详细见附件
附件
1: 1.txt (2006-11-7 01:48, 2.39 KiB, 下载附件所需积分 1 点
,下载次数: 19)
|
|
2006-11-7 01:48 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
不好意思,发帖子的时候,copy了修改前的代码,而没有帖出修正后的内容,导致3F的代码漏掉了写入首行和尾行的语句,现已修正,请再次测试。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-7 01:59 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
test.bat
@echo off
::%1为life,调换%2和%3
::比如说在test.txt中调换1行和9行,使用方法test.bat test.txt,1,9
::暂时不能换3,-3之类的,不过可以sed -n "$=" %1 来看总共有多少行~再set/a计算实际的+行数
if *%1==* (echo 未指定文件&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.txt
`-=\;',./~!@#o^&*()_+{}|:"<>?
ab
AB
12 34
一二三四五六七八九○
!·¥……—()——:“?》《
http ://abc.abc.abc/
Last edited by vkill on 2006-11-7 at 09:55 AM ]
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 ]
|
|
2006-11-7 09:07 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
网吧里的系统一般都是被阉割过的,有安全问题的命令可能都被废了。
The systems in internet cafes are generally cut down. The commands with security issues may all be disabled.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-7 09:23 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Originally posted by namejm at 2006-11-7 09:23:
网吧里的系统一般都是被阉割过的,有安全问题的命令可能都被废了。
namejm兄测试下,原理是先把9替换为1,再把1复制为原文件9的内容~
11 F代码更新
Last edited by vkill on 2006-11-7 at 09:58 AM ]
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 ]
|
|
2006-11-7 09:31 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
居然没有保存 sed 程序,我知道无奈何兄最近刚发过,但一时也搜索不到它的下载,汗~~,能给个下载的地址吗?
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-7 10:24 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
我也补个 sed 的,文件不要过于巨大。
sed -re :a -e "N;$!ba;s/(*)\n(.*)\n(.*)/\3\n\2\n\1/"
sed 下载:http://www.student.northpark.edu/pemente/sed/gsed407x.zip
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
|
|
2006-11-7 11:09 |
|