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-21 22:44
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Question: Organize the text (newcomers and veterans are all welcome to try) View 6,071 Replies 54
Floor 16 Posted 2008-07-13 22:10 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Originally posted by 26933062 at 2008-7-13 21:59:

It wasn't broken at the "中" character; it was automatically wrapped by the forum.
So in my sample, empty lines represent line breaks. Does that make sense?



Oh, then I also said that for the bat, it's up to you to show...

For the VBS, there are only two sentences, and I've already mentioned it... If you want to use VBS, you just need to complete the reading and writing yourself...
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 17 Posted 2008-07-13 22:11 ·  中国 广东 深圳 电信
初级用户
Credits 80
Posts 30
Joined 2007-04-19 23:21
19-year member
UID 85763
Gender Male
Status Offline
```
@echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (a.txt) do (
set "string=%%a"&set "str=%%a"
if "!str:~0,1!"=="。" echo 。&echo.
call :loop
if "!str:~-1!"=="。" echo 。&echo.
)
:loop
for /f "tokens=1,* delims=。" %%i in ("!string!") do (
set string=%%j
if defined string (
set/p=%%i。<nul
echo.&echo.
goto loop
) else (
set/p=%%i<nul
)
)

```
The code is a bit complicated
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
26933062 +15 2008-07-15 15:31
Floor 18 Posted 2008-07-13 22:16 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
The idea on the 14th floor is far - fetched? You're so amazing. Admire you. Completely don't understand. Still take a look at the pure batch file.
致精致简!
Floor 19 Posted 2008-07-13 22:23 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
The person on the 17th floor should have got full marks. It's a pity I can't rate today, I'll make it up tomorrow.

My code. The idea is similar to the 17th floor.


@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in (a.txt) do call :loop "%%i"
pause>nul
:loop
set "str=%~1"
for /f "tokens=1* delims=。" %%a in ("!var!%~1") do (
if "%%b"=="" (
if "!str:~-1!"=="。" (echo %%a。&echo.&set var=) else set "var=!var!%~1"
goto :eof
) else echo %%a。&echo.&set var=&call :loop "%%b"
)
goto :eof
致精致简!
Floor 20 Posted 2008-07-13 22:24 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Although I can't understand the code on floor 14, I think it doesn't meet the LZ's requirements. From the last few lines, it should be that all 0D0A (carriage return line feed) are replaced with empty, that is, the entire text content is merged into one line, and then the periods are replaced with carriage return line feeds. This goes against the original intention of the LZ. However, the code itself is still worthy of our study. I wonder if brother qzwqzw can explain it?
Floor 21 Posted 2008-07-13 23:33 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Actually, according to the original poster's intention, originally using text replacement tools like change, sed, etc. was efficient and highly compatible. But for some reason, the original poster must have a pure P solution. So I had to compromise a bit. Use ASCII Assembler to generate a text replacement tool, and then use it to replace the source text. As for whether it is purely P, there is no need to discuss it too much.
------------------------------------------------------------------

For details about ASCII Assembler, search the past posts (by content).

The following is the usage instructions and source code of sbs2:

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Usage: sbs2.com NUMBER "STRING1" "STRING2" <infile >outfile
::
:: infile and outfile must be files (to allow random access), so in
:: NT/W2000/XP no pipe (program1 | sbst | program2) must be used!
:: infile and outfile must not be the same file!
::
:: Substitutes the NUMBER ocurrence of STRING1 in infile by
:: STRING2 and writes the result to outfile
::
:: You can include any character in STRING1/2 by using its
:: hex value (e.g. $0d for <CR> or $1a for EOF)
::
:: If NUMBER = 0 all STRING1 are substituted by STRING2
::
:: If an error is detected or nothing is substituted, erorrlevel=0
:: replace the original file only if errorlevel>=1 (=number of
:: substitutions).
::
:: Instead of the double quotes (") you can also use single quotes (')
:: with a different meaning for string2:
:: "string2" : normal substitution
:: 'string2" : before substitution the output file is rewinded
:: "string2' : after substitution the outputfile is closed

some examples:

-----------------------------------------------------
Substitutes the 2. occurrence of user by Susan
-----------------------------------------------------
sbs2.com 2 "user" "Susan" <%1 >_._
if errorlevel 1 copy _._ %1
del _._

-----------------------------------------------------
Converts dos textfiles to unix textfiles
-----------------------------------------------------
sbs2.com 0 "$0d" "" <%1 >_._
if errorlevel 1 copy _._ %1
del _._

-----------------------------------------------------
Converts unix textfiles to dos textfiles
-----------------------------------------------------
sbs2.com 0 "$0a" "$0d$0a" <%1 >_._
if errorlevel 1 copy _._ %1
del _._

-----------------------------------------------------
echo without CRLF
-----------------------------------------------------
echo set a=|sbs2.com 0 "$0d$0a" "" >_.bat

-----------------------------------------------------
Remove trailing blanks from all lines
-----------------------------------------------------
:start
sbs2.com 0 " $0d" "$0d" <%1 >_._
if not errorlevel 1 goto ende
copy _._ %1
goto start
:ende
del _._

-----------------------------------------------------
Remove all single CR LF (ascii text file -> WORD)
-----------------------------------------------------
sbs2.com 0 "$0d$0a$0d$0a" "$0a" <%1 >_._
if errorlevel 1 copy _._ %1
sbs2.com 0 "$0d$0a" "" <%1 >_._
if errorlevel 1 copy _._ %1
sbs2.com 0 "$0a" "$0d$0a" <%1 >_._
if errorlevel 1 copy _._ %1
del _._

-----------------------------------------------------
Extract line 5-9 from a text file
-----------------------------------------------------
sbs2.com 4 "$0d$0a" '" <%1 >_._
if errorlevel 1 copy _._ %1
sbs2.com 5 "$0d$0a" "$0d$0a' <%1 >_._
if errorlevel 1 copy _._ %1

-----------------------------------------------------
The source code
-----------------------------------------------------

@=$100
move.w #$81,r5
movu.bw -1.b(r5.w),r2
eor.l r0,r0
eor.l r4,r4

.lab1:
_10: dec.w r2
bmi.w x100
move.b (r5.w),r0
inc.w r5
cmp.b #'0',r0
blo.b _20
cmp.b #'9',r0
bhi.b _20
sub.b #'0',r0
mulsq.l #10,r4,r4
add.l r0,r4
move.w #_30,lab1
br.b _10
_20: jmp.w (lab1)

_30: move.l r4,number

move.w #buf1,r6
bsr.w get_string
or.w r6,r6
beq.b .x100
move.w #buf2,r6
bsr.w get_string

eor.l r5,r5
_a00: move.w #buf1,r6
eor.w r4,r4

_a10: move.b #$3f,m0
move.w #buf0,r1
move.w #1,r2
eor.w r3,r3
trap #$21
bcs.b .x100
or.w r0,r0
beq.b _x100

move.b buf0,r0
cmp.b (r6.w),r0
beq.b _a20

_a50: or.w r4,r4
beq.b _a30
move.w #$4201,r0
move.w #-1,r2
move.w r4,r1
neg.w r1
trap #$21
move.w #buf1,r1
move.w #1,r2

_a30: move.b #$40,m0
inc.w r3
trap #$21
bcs.b .x100
cmp.w #buf2,r1
bne.b _a60
tst.b #1,stop
bne.b _x110
_a60: cmp.w r0,r2
beq.b _a00
.x100: br.b x100

_a20: inc.w r6
inc.w r4
cmp.w buf1l,r4
blo.b _a10
inc.l r5
move.l number,r0
or.l r0,r0
beq.b _a40
cmp.l r0,r5
beq.b _a40
dec.w r4
move.w #buf1,r1
move.w #1,r2
br.b _a50

_a40:
tst.b #1,start
beq.b _a41
move.w #$4200,r0
eor.w r1,r1
eor.w r2,r2
inc.w r3
trap #$21
move.b #$40,m0
trap #$21
dec.w r3
_a41: move.w #buf2,r1
move.w buf2l,r2
br.b _a30

_x100: or.w r4,r4
beq.b _x110
move.w #buf1,r1
move.w r4,r2
br.b _a30
_x110:
move.w r5,r0
or.l r5,r5
beq.b x101
move.l number,r1
or.l r1,r1
beq.b _x120
cmp.l r5,r1
bhi.b x100
move.b #1,r0
br.b x101
_x120: cmp.l #$ff,r5
bls.b x101
move.b #-1,r0
br.b x101
x100: eor.b r0,r0
x101: move.b #$4c,m0
trap #$21

get_string:
eor.w r1,r1
move.w r6,r3
move.w #_10,lab1
move.w #_30,lab2
; move.l #(_30<<16)+_10,lab1 ; kuerzer
_10: dec.w r2
bmi.b _x100
move.b (r5.w),r0
inc.w r5
eor.b m0,m0
cmp.b #'"',r0
beq.b _20
cmp.b #"'",r0
beq.b _21
jmp.w (lab1)
_21: inc.b m0
_20: jmp.w (lab2)
_30: move.w #_40,lab1
move.w #_50,lab2
; move.l #(_50<<16)+_40,lab1 ; kuerzer
move.b m0,start
br.b _10
_40: or.b r1,r1
bne.b _41
cmp.b #'$',r0
bne.b _42
move.w #2,r1
br.b _10
_42: move.b r0,(r6.w)
inc.w r6
br.b _10
_41: or.b #$20,r0
sub.b #'a'-10,r0
bpl.b _43
add.b #'a'-10-'0',r0
_43: lsl.b #4,m1
add.b m1,r0
dec.b r1
beq.b _42
move.b r0,m1
br.b _10

_50: sub.w r3,r6
move.w r6,-2.b(r3.w)
move.b m0,stop
rts.w

_x100: addq.w #2,r7
br.b x100
lab1: dc.w .lab1
lab2: blk.w 1

even
start: blk.b 1
stop: blk.b 1
number: blk.l 1
buf0: blk.w 1
buf1l: blk.w 1
buf1: blk.w 200
buf2l: blk.w 1
buf2: blk.w 200
Floor 22 Posted 2008-07-14 00:27 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Feels like the thinking and efficiency are still okay, so I posted it:

@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in (a.txt) do (set str=%%i
if "!str:。=!" neq "!str!" call :lp
if defined str set /p=!str:。=!<nul
)
pause>nul&goto :eof
:lp
set var=%str%
if "%str:~,1%" equ "。" set /p=。<nul&echo.&echo.
set str=%str:。= %
if "%var:~-1%" neq "。" set str=%str%。
for %%i in (%str%) do (set var=%%i
if "!var:~-1!" neq "。" set /p=%%i。<nul&echo.&echo.&set str=!str:%%i=!
)


[ Last edited by zw19750516 on 2008-7-14 at 12:28 AM ]
批处理之家新域名:www.bathome.net
Floor 23 Posted 2008-07-14 09:20 ·  中国 山东 聊城 联通
初级用户
★★
Credits 73
Posts 51
Joined 2007-08-31 10:04
18-year member
UID 96241
Gender Male
Status Offline
Xiaocai is learning here. Also congratulations to ZW19750516 for being promoted to Gold Member.
Floor 24 Posted 2008-07-14 09:31 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Brother qzwqzw, can you write one in sed version? I want to learn.
What I wrote doesn't work. The part after the pipe doesn't seem to take effect.
sed "s/。/。\n/g" a.txt | sed "/。$/!s/\n//g"
Floor 25 Posted 2008-07-14 15:03 ·  中国 湖北 武汉 联通
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
sed "s/。/&\n\n/g" myfile.txt | sed ":nt; /。$\|^$/!{N;bnt};s/\n//g"
Floor 26 Posted 2008-07-14 15:28 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Originally posted by lxmxn at 2008-7-14 15:03:
sed "s/。/&\n\n/g" myfile.txt | sed ":nt; /。$\|^$/!{N;bnt};s/\n//g"

I've racked my brains and still can't figure it out.
Can the moderator explain this command?
In "/。$\|^$/", is the pipe here meaning "or"? Then the preceding \ is an escape character?
Your code will display an extra blank line when the line starts with a period. I modified it as follows. I wonder if it will go wrong in some cases?
Also: Can it be solved without using the pipe symbol?

sed "s/。/&\n\n/g" a.txt | sed ":nt; /。$/!{N;b nt};s/\n//g;n"


[ Last edited by 26933062 on 2008-7-14 at 03:29 PM ]
致精致简!
Floor 27 Posted 2008-07-14 15:43 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
The moderator is very smart, I've learned.
But I found that this will add an extra blank line after the line that originally ends with a period. The modification is as follows:
sed "s/。/&\n\n/g" a.txt | sed "/^$/{:a;N;s/^\n$//;ta}" | sed ":a; /。$\|^$/!{N;ba};s/\n//g"
Floor 28 Posted 2008-07-14 15:47 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
\ is an escape character, | is not a pipe, but "or"
/.\$\|\^$/! means to execute the following command on non - empty lines that do not end with a period
Floor 29 Posted 2008-07-14 15:53 ·  中国 山西 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
I'm sorry, correct a sentence.

The most concise, efficient and general solution to realize the building owner's intention is to use full-text text replacement tools such as change, hexc, etc.
Rather than text editing tools in line-buffered form such as sed/awk,
Because the grammar of the latter will be too obscure when dealing with cross-line problems,
And the compatibility is not very high,
Because there are great differences in the support of many non-standard marks among multiple implementations of sed and awk.

I know very little about sed.
Basically, I only know the part of regular expressions.
I haven't been exposed to others.
But I have doubts about the compatibility of marks like :nt, {N;b nt}, etc.

[ Last edited by qzwqzw on 2008-7-14 at 03:55 PM ]
Floor 30 Posted 2008-07-14 15:53 ·  中国 湖北 武汉 联通
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Well, it just means "or". Actually, I'm not proficient in sed either, I just made it up randomly...
Forum Jump: