|
liuwei723
新手上路

积分 18
发帖 5
注册 2006-4-26
状态 离线
|
『楼 主』:
如何在批处理中实现对文本文档的每一行开头或者末尾加入
使用 LLM 解释/回答一下
我想在一个文本文档的每一行的开头和结尾加上特定的字符串,请问如何实现?
Last edited by liuwei723 on 2006-4-26 at 15:09 ]
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 ]
|
|
2006-4-26 15:08 |
|
|
bush
银牌会员
    
积分 2165
发帖 730
注册 2004-4-21
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
dos下,好像这个只有用第三方工具。 vbs的倒好容易。
我以前都是用change.exe来处理文字替换,行首插入稍微麻烦一点
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.
|
|
2006-4-28 00:51 |
|
|
Wengier
系统支持
             “新DOS时代”站长
积分 27736
发帖 10521
注册 2002-10-9
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
DOS下倒是可以试试lmod工具来实现,比如说:
type in.txt | lmod start [] end > out.txt
这样就可以在in.txt中每行的开头和结尾加入字符串,并将结果输入到out.txt
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和雨露,谢谢!)
 |
|
2006-4-28 01:12 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
@echo off
setlocal enabledelayedexpansion
set mm=%1
set /p start=输入每行开头要添加的字符串:
set /p end=输入每行结为要添加的字符串:
for /f "tokens=1,2* delims=:" %%i in ('findstr /n . %mm%') do echo ^!start^!%%j^!end^! >>22.txt
endlocal
前几天看过无奈何兄的那个前后加符号的回帖,上面的可以过滤空白行,不过生成的文件把以前的空白行删除了,不知道空白行对楼主有用还是没用,还有就是对于" & <"这些字符的处理问题,ECHO命令是不能直接写入生成的,所以需要用^前导字符来进行显示,如果楼主没有其他要求,这个应该差不多也许大概应该可以完成基本的使用了!!!!!!!!
@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!!!!!
|
|
2006-4-28 11:44 |
|
|
liuwei723
新手上路

积分 18
发帖 5
注册 2006-4-26
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
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 ...
呵呵,谢谢。几位兄弟给我写的代码,我都仔细研究过,总算可以达到要求了。
以前由于对批处理接触的少,所以这方面就不太了解,研究了两天,总算学到了些东西,对批处理有了进一步的了解。现在就是对FOR和SET没有完全搞明白。其帮助文件说的也不太明白,而对于这方面的一些讨论,大多也只有怎么写代码,没有解释其含义的。不知谁能够提供给我一个好点的东西,可以再深层学习一下。
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.
|
|
2006-4-28 19:22 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
|
2006-4-30 12:37 |
|
|
liuwei723
新手上路

积分 18
发帖 5
注册 2006-4-26
状态 离线
|
|
2006-4-30 17:55 |
|
|
khw
新手上路

积分 10
发帖 5
注册 2006-5-2
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
我想在一個文本文檔的每一字後面加上特定的字符串,請問如何實現?
如:
可愛的太陽啊
微微的春風吹
變成:
可-愛-的-太-陽-啊-
微-微-的-春-風-吹-
求各位高手幫忙處理!!!!
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!!!!
|
|
2006-5-2 01:32 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Re khw:
如果纯粹地用批处理来解决的在效率以及容错上面不是很理想,而且相对比较繁琐,建议用vbs解决:
SFile = GetFileName()
Msg = "替换字符格式为:" & vbtab & "str1:=str2" & vblf
Msg = Msg & "省略str2可以删除str1"
Str = inputbox(Msg,"输入要插入的字符:")
if len(Str) = 0 then Wscript.Quit
'先把文件读到StrOld中,然后把处理后的字符串StrNew写回到文件
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
'插入字符函数,依次读入每个字符并判断是否为空格,回车,换行
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
'获得被操作文件名
Function GetFileName()
if WScript.Arguments.Count <> 0 Then
GetFileName = Wscript.Arguments(0)
else
with CreateObject("UserAccounts.CommonDialog")
.Filter = "文本文件|*.txt|所有文件|*.*"
.ShowOpen
GetFileName = .Filename
end with
end if
if len(trim(GetFileName)) = 0 then Wscript.Quit
End Function
保存为 字符处理.vbs 后把要处理的文件拖到它上面或直接双击运行都可以,在弹出的对话框中输入你想插入的符号。
不光可以插入符号,还可以删除和替换符号,具体不足之处自己看着改改吧,windows xp pro sp2下测试通过。
ps:建议单独的问题单独发一个贴。
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.
|
|
2006-5-2 08:59 |
|
|
khw
新手上路

积分 10
发帖 5
注册 2006-5-2
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
3742668版主您好 :
我已測試ok,非常感謝您的詳細解答,真是佩服佩服!
khw
Hello moderator 3742668:
I have tested and it's okay. Thank you very much for your detailed explanation. I really admire it!
khw
|
|
2006-5-2 14:55 |
|
|
doscc
中级用户
  
积分 256
发帖 93
注册 2006-3-26 来自 广东
状态 离线
|
|
2006-5-2 18:01 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
|
2006-11-4 07:21 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
在批处理中,对于空行的问题,似乎可以这样解决:
将原来的for /f "tokens=1,2* delims=:" %%i in ('findstr /n . 1.txt')
改成for /f "tokens=1,2* delims=:" %%i in ('findstr /n .* 1.txt')
@echo off
setlocal enabledelayedexpansion
set /p start=输入每行开头要添加的字符串:
set /p end=输入每行结为要添加的字符串:
for /f "tokens=1,2* delims=:" %%i in ('findstr /n .* 1.txt') do echo ^!start^!%%j^!end^!>>22.txt
pause>nul
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
|
|
2006-11-4 09:58 |
|
|
suli2921
新手上路

积分 2
发帖 1
注册 2007-7-5
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
Originally posted by vkill at 2006-11-4 07:21 AM:
这个问题sed 解决最好,我感觉~任何字符都经过测试,一句话就可以
sed "s/^/\/span>/;s/\x24/\/span>/" test.txt
sed下载:http://www.student.northpark ...
替换的内容能写到原来的文件中吗?
我试了一下,只能在命令行窗口中显示,
不会用 sed,还望指教。
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.
|
|
2007-7-5 15:43 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
现在的sed 有个 -i 选项 可以直接作用于 源文件;
实际上 是它自动替你完成了 重定向到临时文件 然后 替换文件的操作.
lz的问题 在vim 里的 可视化块 操作里 瞬间就可以完成;
选个好的编辑器 要省时省力的多啊
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.
|
|
2007-7-5 22:43 |
|