中国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-26 01:19
47,812 topics / 349,910 posts / today 0 new / 48,264 members
DOS批处理 & 脚本技术(批处理室) » How to add at the beginning or end of each line of a text document in batch processing
Printable Version  2,119 / 14
Floor1 liuwei723 Posted 2006-04-26 15:08
新手上路 Posts 5 Credits 18
I want to add specific strings at the beginning and end of each line in a text document. How to achieve this?

[ Last edited by liuwei723 on 2006-4-26 at 15:09 ]
Floor2 bush Posted 2006-04-28 00:51
银牌会员 Posts 730 Credits 2,165
Under DOS, it seems that this can only be done with third-party tools. The VBS one is easy.

I used to use change.exe for text replacement, and inserting at the beginning of lines was a bit troublesome.
Floor3 Wengier Posted 2006-04-28 01:12
系统支持 Posts 10,521 Credits 27,736
Under DOS, you can try using the lmod tool. For example:

type in.txt | lmod start [] end > out.txt

In this way, strings can be added to the beginning and end of each line in in.txt, and the result is input to out.txt
Floor4 bagpipe Posted 2006-04-28 11:44
银牌会员 Posts 425 Credits 1,144 From 北京
@echo off
setlocal enabledelayedexpansion
set mm=%1
set /p start=Enter the string to add at the beginning of each line:
set /p end=Enter the string to add at the end of each line:
for /f "tokens=1,2* delims=:" %%i in ('findstr /n . %mm%') do echo !start!%%j!end! >>22.txt
endlocal
I saw Brother Wunaike's post about adding symbols before and after a few days ago. The above one can filter blank lines, but the generated file deletes the previous blank lines. I don't know if the blank lines are useful to the original poster or not. Also, there is the issue of handling characters like " & <". The ECHO command cannot directly write to the generated file, so the ^ leading character is needed for display. If the original poster has no other requirements, this should be almost able to complete the basic use!!!!!
Floor5 liuwei723 Posted 2006-04-28 19:22
新手上路 Posts 5 Credits 18
Originally posted by bagpipe at 2006-4-28 11:44:
@echo off
setlocal enabledelayedexpansion
set mm=%1
set /p start=输入每行开头要添加的字符串:
set /p end=输入每行结为要添加的字符串:
for /f "tokens=1,2* delim ...

Hehe, thanks. I have carefully studied the codes written by several brothers, and finally can meet the requirements.
In the past, because I had little contact with batch processing, I didn't understand this aspect well. After researching for two days, I finally learned some things and had a further understanding of batch processing. Now I just haven't fully understood FOR and SET. The help files don't explain it very clearly, and for some discussions on this aspect, most of them only have how to write the code, without explaining the meaning. I wonder if anyone can provide me with a better thing to learn more deeply.
Floor6 bagpipe Posted 2006-04-30 12:37
银牌会员 Posts 425 Credits 1,144 From 北京
http://www.cn-dos.net/forum/viewthread.php?tid=19331&fpage=1&highlight=%2Bbagpipe
There seem to be many command introductions in the forum. Take a good look by yourself, mainly practice, and the above may be helpful to you
Floor7 liuwei723 Posted 2006-04-30 17:55
新手上路 Posts 5 Credits 18
Originally posted by bagpipe at 2006-4-30 12:37:
http://www.cn-dos.net/forum/viewthread.php?tid=19331&fpage=1&highlight=%2Bbagpipe
There seem to be many introductions to commands in the forum. Take a good look yourself, mainly practice it. The above...

Hehe, thank you~ Later, I specially studied for carefully and did some practice, and finally figured out how the several parameters of for work. This article is really good. If I had read it earlier, I would have understood it earlier. I saved it, hehe.
Floor8 khw Posted 2006-05-02 01:32
新手上路 Posts 5 Credits 10
I want to add a specific string after each character in a text document. How to achieve it?

For example:
Lovely sun ah
Slight spring breeze blows
Become:
Ke-ai-de-tai-yang-a-
Wei-wei-de-chun-feng-chui-

Please help from all the experts!!!!
Floor9 3742668 Posted 2006-05-02 08:59
荣誉版主 Posts 718 Credits 2,013
Re khw:
If purely using batch processing to solve it, the efficiency and fault tolerance are not very ideal, and it is relatively cumbersome. It is recommended to use VBS to solve it:

Save it as "Character Processing.vbs". Then you can drag the file to be processed onto it or double-click to run it. In the pop-up dialog box, enter the symbol you want to insert.
It can not only insert symbols, but also delete and replace symbols. For specific deficiencies, you can modify it yourself. Tested under Windows XP Pro SP2.
ps: It is recommended to post a separate post for a separate question.
Floor10 khw Posted 2006-05-02 14:55
新手上路 Posts 5 Credits 10
Hello moderator 3742668:

I have tested and it's okay. Thank you very much for your detailed explanation. I really admire it!

khw
Floor11 doscc Posted 2006-05-02 18:01
中级用户 Posts 93 Credits 256 From 广东
Floor12 vkill Posted 2006-11-04 07:21
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
This problem is best solved with sed, I think~ Any characters have been tested, just one sentence can do

sed "s/^/\/span>/;s/\x24/\/span>/" test.txt

Sed download: http://www.student.northpark.edu/pemente/sed/gsed407x.zip
Floor13 youxi01 Posted 2006-11-04 09:58
高级用户 Posts 247 Credits 846 From 湖南==》广东
In batch processing, for the problem of empty lines, it seems that this can be solved:
Change the original for /f "tokens=1,2* delims=:" %%i in ('findstr /n . 1.txt')
to for /f "tokens=1,2* delims=:" %%i in ('findstr /n .* 1.txt')

@echo off
setlocal enabledelayedexpansion
set /p start=Enter the string to add at the beginning of each line:
set /p end=Enter the string to add at the end of each line:
for /f "tokens=1,2* delims=:" %%i in ('findstr /n .* 1.txt') do echo ^!start^!%%j^!end^!>>22.txt
pause>nul
Floor14 suli2921 Posted 2007-07-05 15:43
新手上路 Posts 1 Credits 2
Originally posted by vkill at 2006-11-4 07:21 AM:
This problem is best solved with sed, I think~ Any characters have been tested, one sentence can do it

sed "s/^/\/span>/;s/\x24/\/span>/" test.txt

Sed download: http://www.student.northpark ... Can the replaced content be written back to the original file? I tried it, but it only shows in the command line window, I don't know how to use sed, please give me some advice.
Floor15 bjsh Posted 2007-07-05 22:43
银牌会员 Posts 621 Credits 2,000
Now the current sed has an -i option that can directly act on the source file;

In fact, it automatically completes the operation of redirecting to a temporary file and then replacing the file for you.


The problem of the LZ can be completed in an instant in the visual block operation in vim;

Choosing a good editor is much more time-saving and labor-saving.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023