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 04:58
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to use batch to swap two lines in a TXT text? View 4,653 Replies 24
Floor 16 Posted 2006-11-09 02:11 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
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 无奈何 at 2006-11-7 11:09:
I'll also add one for sed, but the file shouldn't 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/s ...

The moderator's sed usage is not understandable, I'll copy it back and check the information first
Floor 17 Posted 2006-11-09 10:50 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline

@echo off
setlocal ENABLEDELAYEDEXPANSION
set file=a.txt
for /f "delims=: tokens=1,*" %%i in ('findstr /n .* "%file%"') do set "%%i=%%j" & set num=%%i
set "str=!%~1!"
set "%~1=!%~2!"
set "%~2=%str%"
for /l %%i in (1,1,%num%) do if defined %%i (echo.!%%i!) else echo.
pause

Using the above principle, it's not difficult to implement 3, -3.
I was writing the above thing when a friend called me to help with the system. I don't know if there are bugs, but the principle is just those.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +4 2006-11-09 11:32
Floor 18 Posted 2006-11-09 11:47 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
The method that the moderator let the line numbers directly simulate array variables is really clever~:)
It's so enjoyable to be able to see such interesting content~:)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 19 Posted 2006-11-11 07:55 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
I'm wondering if the code of moderator 3742668 will be executed when encountering | in the line. I tried and it seems not possible
Floor 20 Posted 2006-11-13 06:40 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
I'm wondering if the code of moderator 3742668 will execute when encountering | in the line ~ I tried and it seems not possible

Any parameters added?
It tests normally here.
Floor 21 Posted 2007-02-02 16:33 ·  美国 北达科他州立大学
中级用户
★★
Credits 316
Posts 152
Joined 2006-06-18 13:01
20-year member
UID 57204
Gender Male
Status Offline
There's no help for it that the code of the moderator is concise and neat when dealing with small texts;
The code of vkill will go wrong when there is no carriage return (newline) character in the last line of the text;
I'll also post my code, based on sed

@echo off
sed -n "$!d;G;P" a.txt> temp.txt
sed "1d;$d" a.txt>> temp.txt
sed q a.txt>> temp.txt
type temp.txt
pause

It works no matter whether there is a carriage return (newline) character in the last line of the text or not

[ Last edited by amao on 2007-2-2 at 05:18 PM ]
Floor 22 Posted 2007-02-03 00:02 ·  中国 吉林 四平 联通
高级用户
★★★
Credits 859
Posts 413
Joined 2006-08-14 21:55
19-year member
UID 60532
Status Offline
It's really not easy to be able to write the requirements like this
Floor 23 Posted 2007-02-03 05:06 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
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 amao at 2007-2-2 16:33:
There is no help for it, the moderator's code is concise and neat when solving small texts;
vkill's code will have an error when there is no carriage return (newline) character in the last line of the text;
I will also post my code, based on sed

@ech ...

Brother also likes sed? Hehe
Floor 24 Posted 2007-02-03 11:06 ·  美国 缅因州
中级用户
★★
Credits 316
Posts 152
Joined 2006-06-18 13:01
20-year member
UID 57204
Gender Male
Status Offline
Originally posted by vkill at 2007-2-3 05:06:

You also like sed? Hehe

I'm studying it hard
Floor 25 Posted 2007-11-29 23:22 ·  中国 广东 广州 广东金万邦科技投资有限公司(新一代数据中心)IDC机房(BGP)
新手上路
Credits 4
Posts 2
Joined 2007-11-29 21:28
18-year member
UID 104196
Gender Male
Status Offline
Bump up to ask. If I want to swap the lines containing "Zhang San" and "Xiao Er" according to the text content given by the LZ, how to handle it? I want to hear the processing method with sed :)
Forum Jump: