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-27 04:40
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to add at the beginning or end of each line of a text document in batch processing View 2,062 Replies 14
Original Poster Posted 2006-04-26 15:08 ·  中国 湖北 武汉 电信
新手上路
Credits 18
Posts 5
Joined 2006-04-26 15:04
20-year member
UID 54491
Status Offline
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 ]
Floor 2 Posted 2006-04-28 00:51 ·  中国 重庆 渝中区 电信
银牌会员
★★★
Credits 2,165
Posts 730
Joined 2004-04-21 00:00
22-year member
UID 22966
Gender Male
Status Offline
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.
Floor 3 Posted 2006-04-28 01:12 ·  加拿大 Bell
系统支持
★★★★★★
“新DOS时代”站长
Credits 27,736
Posts 10,521
Joined 2002-10-09 12:00
23-year member
UID 9
Status Offline
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
Wengier - 新DOS时代

欢迎大家来到我的“新DOS时代”网站,里面有各类DOS软件和资料,地址:
http://wendos.mycool.net/

E-Mail & MSN: wengierwu AT hotmail.com (最近比较忙,有事请联系DOSroot和雨露,谢谢!)

Floor 4 Posted 2006-04-28 11:44 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
@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!!!!!
Floor 5 Posted 2006-04-28 19:22 ·  中国 湖北 武汉 电信
新手上路
Credits 18
Posts 5
Joined 2006-04-26 15:04
20-year member
UID 54491
Status Offline
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.
Floor 6 Posted 2006-04-30 12:37 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
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
Floor 7 Posted 2006-04-30 17:55 ·  中国 湖北 武汉 电信
新手上路
Credits 18
Posts 5
Joined 2006-04-26 15:04
20-year member
UID 54491
Status Offline
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.
Floor 8 Posted 2006-05-02 01:32 ·  中国 台湾 中华电信(HiNet)数据中心
新手上路
Credits 10
Posts 5
Joined 2006-05-02 01:14
20-year member
UID 54810
Gender Male
Status Offline
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!!!!
Floor 9 Posted 2006-05-02 08:59 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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:

SFile = GetFileName()
Msg = "The replacement character format is:" & vbtab & "str1:=str2" & vblf
Msg = Msg & "Omit str2 can delete str1"
Str = inputbox(Msg,"Enter the character to insert:")
if len(Str) = 0 then Wscript.Quit

'First read the file into StrOld, then write the processed string StrNew back to the file
with CreateObject("Scripting.FileSystemObject")
set F = .OpenTextFile(SFile,1,True)
StrOld = F.ReadAll
set F = Nothing
set F = .OpenTextFile(SFile,2,True)
if len(Str) > 1 and instr(Str,":=") then
StrArray = split(Str,":=")
StrNew = Replace(StrOld,StrArray(0),StrArray(1))
else
StrNew = InertStr()
end if
F.Write(StrNew)
end with

'Insert character function, read each character in turn and judge whether it is space, carriage return, line feed
Function InertStr()
for i = 1 to len(StrOld)
if mid(StrOld,i,1) = space(1) or _
asc(mid(StrOld,i,1)) = 13 or _
asc(mid(StrOld,i,1)) = 10 then

strTmp = strTmp & mid(StrOld,i,1)
else
strTmp = strTmp & mid(StrOld,i,1) & Str
end if
next
InertStr = strTmp
End Function

'Get the name of the file to be operated
Function GetFileName()
if WScript.Arguments.Count <> 0 Then
GetFileName = Wscript.Arguments(0)
else
with CreateObject("UserAccounts.CommonDialog")
.Filter = "Text files|*.txt|All files|*.*"
.ShowOpen
GetFileName = .Filename
end with
end if
if len(trim(GetFileName)) = 0 then Wscript.Quit
End Function

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.
Floor 10 Posted 2006-05-02 14:55 ·  中国 台湾 中华电信
新手上路
Credits 10
Posts 5
Joined 2006-05-02 01:14
20-year member
UID 54810
Gender Male
Status Offline
Hello moderator 3742668:

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

khw
Floor 11 Posted 2006-05-02 18:01 ·  中国 上海 华为云
中级用户
★★
Credits 256
Posts 93
Joined 2006-03-26 22:12
20-year member
UID 52853
Gender Male
From 广东
Status Offline
Floor 12 Posted 2006-11-04 07:21 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
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
Floor 13 Posted 2006-11-04 09:58 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
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
Floor 14 Posted 2007-07-05 15:43 ·  中国 北京 中电华通通信有限公司
新手上路
Credits 2
Posts 1
Joined 2007-07-05 14:29
18-year member
UID 93222
Gender Male
Status Offline
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.
Floor 15 Posted 2007-07-05 22:43 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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.
Forum Jump: