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-08-12 17:29
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to replace the content of certain lines in a text file, including special characters and blank lines View 1,444 Replies 7
Original Poster Posted 2009-05-04 05:36 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-04-01 23:00
17-year member
UID 141837
Gender Male
Status Offline
For example, I have a text file a.txt under d:\ad\, and the content is as follows:

This is a dos
a - dos bat.
like bat

output: ok
Now I need to replace the blank lines between line 2, line 3 and line 4 with "join us". How to handle it? Thanks!

[ Last edited by mailmail on 2009-5-4 at 23:41 ]
Floor 2 Posted 2009-05-04 05:39 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
What you said is about blank lines or spaces?

有问题请发论坛或者自行搜索,再短消息问我的统统是SB
Floor 3 Posted 2009-05-04 07:05 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-04-01 23:00
17-year member
UID 141837
Gender Male
Status Offline
a DOS batch file.
Like a batch file
Add the following blank line, all replaced with one sentence
Floor 4 Posted 2009-05-04 09:06 ·  美国 新泽西州
新手上路
Credits 10
Posts 10
Joined 2009-04-01 23:00
17-year member
UID 141837
Gender Male
Status Offline
I'm sorry, I made a mistake. It should be a direct carriage return and line feed, that is, an empty line with nothing in it, which is equivalent to taking the following text content

This is a dos
a - dos bat.
like bat

output: ok
Change to the following form
This is a dos
join us.
output: ok
Floor 5 Posted 2009-05-04 23:41 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-04-01 23:00
17-year member
UID 141837
Gender Male
Status Offline
I have found relevant examples in the forum and saw how to replace a certain line
http://www.cn-dos.net/forum/viewthread.php?tid=35759&fpage=1
But what if I want to replace multiple lines into one line? That is, set string multiple times?
Also, what if there are blank lines among multiple lines, how to replace them?
Floor 6 Posted 2009-05-04 23:51 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
It is generally better to handle it with a regular expression find and replace tool.

fr -rnnlic:"like bat\r\n\r\n" -t:"join us\." a.txt

FR download:
http://baiy.cn/utils/fr/index.htm

有问题请发论坛或者自行搜索,再短消息问我的统统是SB
Floor 7 Posted 2009-05-04 23:57 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-04-01 23:00
17-year member
UID 141837
Gender Male
Status Offline
Thanks! This is for learning. Because it's used in the company, external new commands can't be used. If not using this, can we only use loop statements to replace one by one?
Floor 8 Posted 2009-10-22 20:38 ·  中国 江苏 南京 电信
初级用户
★★
Credits 62
Posts 62
Joined 2007-07-14 14:39
19-year member
UID 93962
Gender Male
Status Offline
Originally posted by yishanju at 2009-5-4 11:51 PM:
It is generally better to use a regular expression find-and-replace tool for processing.

fr -rnnlic:"like bat\r\n\r\n" -t:"join us\." a.txt

FR download:
http://baiy.cn/utils/fr/index.htm


This also has issues and cannot handle processing in a loop. If "like bat" is after a blank line, it cannot find the "like bat" field and process it.

Also, it seems that Brother Yishanju is very proficient in the regular expressions of fr. Why not open a special section to specifically elaborate on it? I looked at Bai Yang's website, which only has rules but lacks example explanations, making it very confusing for beginners.

[ Last edited by pkto on 2009-10-22 at 20:40 ]
Forum Jump: