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-25 00:12
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to split a file by markers using batch processing DigestI View 13,118 Replies 30
Floor 16 Posted 2005-12-09 15:25 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re All:

First of all, issue an announcement:

Because my personal computer has now been moved elsewhere, cut off from the network, for closed training, for at least two weeks. During this period, I can only log in to the forum in Internet cafes or on others' computers during spare time, so I will temporarily resume the online receiving and offline replying internet habits. Therefore, it will inevitably lead to reduced reading frequency, extended reply cycles, and weakened management efforts. Please understand from the management and friends!

Brother tigerpower's described situation, there are exactly the following three feelings, listed as follows: As the ancient saying goes, know that it is so but do not know why it is so; in today's view, understand the result but not necessarily understand the process; in the words of folk sayings, heard that tigers can eat people a lot, but saw how tigers eat people few.

I, regarding awk, can be regarded as "very famous, well-known; but fate plays tricks, missed meeting". This command-line tool originating from the Unix family, I have had occasional exposure during the process of learning batch processing, but always failed to go deep. In addition to the common human sentiment of fear of difficulties and avoiding risks similar to Brother Wunaike, there is also a self-importance of maintaining the pure lineage of batch processing. But I also deeply understand that the value of a tool is to be used by the right person at the right time and in the right place. Blindly avoiding is just the ostrich mentality of covering one's ears to steal a bell. Now, seeing that Brother Wunaike has taken the first step earlier than me, I am happy and a bit ashamed.

Now I have made a decision, instead of standing outside the door to cheer for seeing the excitement, it is better to sit inside the pavilion to clap for recognizing the essence - awk, it's time to get in touch.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 17 Posted 2005-12-09 19:28 ·  中国 浙江 金华 电信
初级用户
Credits 90
Posts 26
Joined 2005-12-05 12:41
20-year member
UID 46522
Status Offline
Got the help from everyone. Thank you so much. Please two of you recommend the most commonly used commands in UNIX for learning to solve many problems in actual work. Pay tribute to the seniors
Floor 18 Posted 2005-12-09 20:34 ·  中国 上海 电信
中级用户
★★
大师兄
Credits 377
Posts 99
Joined 2005-08-26 07:37
20-year member
UID 41945
Status Offline
Oh, I see. I thought the forum moderator had already seen my previous solution using mawk. Actually, I deleted that post because I wanted to see how everyone would solve it with sed.
Floor 19 Posted 2005-12-11 21:58 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re tigerpower:

I haven't seen your solution with mawk. The forum is supposed to be a place where various ideas flourish, so you shouldn't be too stingy with your insights.

This afternoon, I finally had time to read some Chinese awk documentation. Based on my initial understanding, I still responded to the original topic starter and wrote my first awk script. Since there are still some differences from Brother Wunaaihe's script, I'm posting it to start a discussion.

Re 无奈何:

I noticed that in your awk script, you use a temp variable as an intermediate. Compared with print>>, is there a possibility that concatenating string variables and then outputting them all at once is more efficient? I don't know if the topic starter still has the intention to do some testing in this regard? Because it's not easy for me to find a larger - scale test file here.


{
if ($1 !~ /^R\*/) {
print $0>>"temp"
} else {
close("temp")
system("ren temp "substr($0,3)".txt")
}
}


[ Last edited by willsort on 2005-12-11 at 22:11 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 20 Posted 2005-12-11 23:43 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
to yhsean
Regarding the "common commands in UNIX" you want to know, I mentioned a toolkit on floor 7. You can find it and play with it.
to willsort
Brother, your learning speed is quite fast! The use of intermediate variables will definitely reduce efficiency, but at that time, I was learning and using it on the spot. I didn't know the close() function, and directly calling the system command to change the file name failed. There was no way, so I had to take a shortcut. But that was my first AWK program, and I don't plan to modify it. It would be best if you can post your complete program to give a perfect solution to this thread's problem.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 21 Posted 2005-12-12 14:19 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
to tigerpower

The speed and stability of mawk are quite remarkable, but unfortunately there are very few materials.

Your code in gawk doesn't pass. I thought your code should be universal, but it passed with a lower version of gawk, which makes me extremely confused.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 22 Posted 2005-12-12 21:24 ·  中国 上海 闵行区 电信
中级用户
★★
大师兄
Credits 377
Posts 99
Joined 2005-08-26 07:37
20-year member
UID 41945
Status Offline
Originally posted by Nai Nai He at 2005-12-12 14:19:
...As a result, switching to a lower version of gawk worked, which really made me dizzy


Haha, I haven't tried gawk yet.
I also use GNU utilities for Win32, but the join in it has a bug, so brothers should be careful when using it!
Floor 23 Posted 2005-12-12 22:12 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
I no longer dare to use the newer versions of GAWK because of their poor stability. Today, while practicing a program, I got bizarre results with the newer versions of GAWK, but the older versions and other versions of AWK worked perfectly. Getting different results with the same script is much more serious than not being able to run through, and it's too hard to accept.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 24 Posted 2005-12-13 08:33 ·  中国 上海 徐汇区 电信
中级用户
★★
大师兄
Credits 377
Posts 99
Joined 2005-08-26 07:37
20-year member
UID 41945
Status Offline
The flagship products of GNU are gcc and emacs, and the code quality and running performance of other software are open to question.
But in most Linux forums, Linux and GNU are hyped up to the skies, with too strong a subjective color.
I also use Linux in daily life. I think the BSD versions of many software are better
Floor 25 Posted 2005-12-13 09:20 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
After reading this post for a long time, my brain is still in a muddle. Unix - related things are really too complicated, not something that my simple brain can understand.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 26 Posted 2005-12-21 12:54 ·  中国 浙江 金华 永康市 电信
初级用户
Credits 90
Posts 26
Joined 2005-12-05 12:41
20-year member
UID 46522
Status Offline
Originally posted by Wú Nài Hé at 2005-12-8 13:16:

BEGIN { FS="\n"}
{
if ($1~/^R.*/) {
name = substr($1,length($1)-1)
temp = substr(temp,2)
print temp>name".txt"
temp = ""
}
else {
temp = (temp "\n" $1)
}
}



Re-digest Brother Wú Nài Hé's script and find that it can be made more concise like this

$0~/^R.*/? { print temp>substr($1,3)".txt"; temp = "" } : temp = (temp $0 )

[ Last edited by yhsean on 2005-12-22 at 13:29 ]
Floor 27 Posted 2005-12-21 14:43 ·  中国 广东 珠海 电信
中级用户
★★
Credits 493
Posts 161
Joined 2002-10-29 00:00
23-year member
UID 94
Gender Male
From ZHCN
Status Offline
A bit confusing. @_@
Floor 28 Posted 2005-12-28 19:41 ·  中国 浙江 金华 浦江县 电信
初级用户
Credits 90
Posts 26
Joined 2005-12-05 12:41
20-year member
UID 46522
Status Offline
I'm sorry, technology is precise. Correcting an error from my last time

{
if ($1 !~ /^R\*/) {
print $0>>"temp"
} else {
close("temp")
system("ren temp "substr($0,3)".txt")
}
}

The code output is faster than using a variable to buffer and output all at once. I think it should be understood like this:

Without using a buffer variable, once processed, the information in memory is released. If a buffer variable is used, if the capacity of the buffer variable is too large, the speed drops sharply. This was the result from testing a 10M text content.

特此更正,以免误导人!(Here is the correction to avoid misleading others!)
Floor 29 Posted 2007-02-12 10:29 ·  中国 甘肃 兰州 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Using sed, in principle it's possible, just for this specific one


@echo off
set life=AA.txt
for /l %%? in (2,1,100) do (
call :test "%%?"
)
exit
:test
set/a var1=%~1-1
set/a var2=%~1-2
type %life%|findstr "^R\**%var1%$" >nul||(goto end)
if defined a (sed "/^R\**%var2%$/,/^R\**%var1%$/!d" %life% |sed "1d;$d"|more>%var1%.txt
) else (
set a=a
sed "1,/^R\**%var1%$/!d" %life% |sed "$d" |more>%var1%.txt
)
goto :eof
:end
sed "/^R\**%var2%$/,$!d" %life% |sed "1d"|more>n.txt
exit
goto :eof


[ Last edited by vkill on 2007-2-12 at 10:42 AM ]
Floor 30 Posted 2007-02-12 11:11 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
Post a VBS one, just for fun

on error resume next
set fso=createobject("scripting.filesystemobject")
set d=createobject("scripting.dictionary")
set file=fso.opentextfile("a.txt")
do while file.atendofstream<>true
m=m+1
d.add m,file.readline
loop
file.close
a=d.items
for i=0 to m-1
if left(a(i),2)<>"R*" then
s=s&a(i)&vbcrlf
h=mid(a(i+1),3)
else
set file=fso.createtextfile(h & ".txt")
file.write s
file.close
s=""
end if
next
msgbox "Successfully split the file by the mark 'R*'.",4096,"System Prompt"


[ Last edited by jmz573515 on 2007-2-11 at 10:35 PM ]
Forum Jump: