中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-12 16:30
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » How to replace the content of certain lines in a text file, including special characters and blank lines
Printable Version  1,443 / 7
Floor1 mailmail Posted 2009-05-04 05:36
新手上路 Posts 10 Credits 10
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 ]
Floor2 yishanju Posted 2009-05-04 05:39
银牌会员 Posts 1,357 Credits 1,488
What you said is about blank lines or spaces?
Floor3 mailmail Posted 2009-05-04 07:05
新手上路 Posts 10 Credits 10
a DOS batch file.
Like a batch file
Add the following blank line, all replaced with one sentence
Floor4 mailmail Posted 2009-05-04 09:06
新手上路 Posts 10 Credits 10
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
Floor5 mailmail Posted 2009-05-04 23:41
新手上路 Posts 10 Credits 10
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?
Floor6 yishanju Posted 2009-05-04 23:51
银牌会员 Posts 1,357 Credits 1,488
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
Floor7 mailmail Posted 2009-05-04 23:57
新手上路 Posts 10 Credits 10
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?
Floor8 pkto Posted 2009-10-22 20:38
初级用户 Posts 62 Credits 62
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 ]
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023