|
qinqong
高级用户
   
积分 599
发帖 148
注册 2003-10-30
状态 离线
|
『楼 主』:
求助!! 怎么样把一个文本内的多行拼接成一行
使用 LLM 解释/回答一下
问题是这样的,有一个文本文件,里而的内容如下
/lib/AdaptiveMQ.jar
/lib/AdaptiveMQ.jar
/lib/FIX.jar
我怎么才能把它放到另外一个文件中,其内容为:
/lib/AdaptiveMQ.jar ;/lib/AdaptiveMQ.jar ;/lib/FIX.jar
The problem is like this. There is a text file with the following content:
/lib/AdaptiveMQ.jar
/lib/AdaptiveMQ.jar
/lib/FIX.jar
How can I put it into another file whose content is:
/lib/AdaptiveMQ.jar ;/lib/AdaptiveMQ.jar ;/lib/FIX.jar
|
|
2006-7-19 13:58 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
这几天也在编一些批处理,学习了一些简单的
@echo off
setlocal EnableDelayedExpansion
set N=1
for /f %%a in (原文本.txt) do (set New!N!=%%a
set /a N=!N! + 1)
echo %New1% ;%New2% ;%New3%>新文本.txt
其中的 %New1%~%NewN% 根据行数增加递增
These days I'm also compiling some batch scripts and have learned some simple ones.
@echo off
setlocal EnableDelayedExpansion
set N=1
for /f %%a in (original text.txt) do (set New!N!=%%a
set /a N=!N! + 1)
echo %New1% ;%New2% ;%New3%>new text.txt
Among them, %New1%~%NewN% increase incrementally according to the number of lines
|
|
2006-7-19 14:12 |
|
|
qinqong
高级用户
   
积分 599
发帖 148
注册 2003-10-30
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
如果我不知道原文本有多少行呢?怎么做急用,谢了
If I don't know how many lines the original text has? What to do? Urgent, thanks.
|
|
2006-7-19 14:15 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (原文件.txt) do (
if not defined a (set a=%%a) else (set a=!a!;%%a)
)
echo %a%>生成文件.txt
可以确定的是,如果文件长度过于太大绝对不成,因为cmd中SET设置变量是有一定要求的,也请高人想想这个批处理怎么写.....................楼主可以试试上面的代码,如果不成就说明文件长度过大
```batch
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (原文件.txt) do (
if not defined a (set a=%%a) else (set a=!a!;%%a)
)
echo %a%>生成文件.txt
```
It can be determined that if the file length is too large, it will definitely not work, because there are certain requirements for the SET setting variable in cmd. Also, please ask the expert to think about how to write this batch processing... The owner can try the above code. If it doesn't work, it means the file length is too large
|
|
2006-7-19 14:56 |
|
|
qinqong
高级用户
   
积分 599
发帖 148
注册 2003-10-30
状态 离线
|
|
2006-7-19 15:01 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
测试完了告诉我一下结果
Last edited by bagpipe on 2006-7-19 at 15:07 ]
Let me know the result after the test is completed.
Last edited by bagpipe on 2006-7-19 at 15:07 ]
|
|
2006-7-19 15:05 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
呵呵,bagpipe又出手了。
if not defined a (set a=%%a) else (set a=!a!;%%a)这句中的defined是什么意思?头一次看到这种用法,能不能把这条代码解释一下?
Hehe, bagpipe has made another move.
What does "defined" mean in the sentence "if not defined a (set a=%%a) else (set a=!a!;%%a)"? I'm seeing this usage for the first time. Can you explain this code?
|
|
2006-7-19 15:05 |
|
|
qinqong
高级用户
   
积分 599
发帖 148
注册 2003-10-30
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
恩,搞定了,好久没用过dos了,都忘的差不多了,多谢 bagpipe的帮忙
Well, it's done. I haven't used DOS for a long time and have almost forgotten it all. Thanks for bagpipe's help.
|
|
2006-7-19 15:21 |
|
|
doscc
中级用户
  
积分 256
发帖 93
注册 2006-3-26 来自 广东
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by namejm at 2006-7-19 15:05:
呵呵,bagpipe又出手了。
if not defined a (set a=%%a) else (set a=!a!;%%a)这句中的defined是什么意思?头一次看到这种用法,能不能把这条代码解释一䠮..
意思就是说. 如果没有定议 a 就 执行 set a=%%a
如果定议了就 执行 else 后的语句!
Originally posted by namejm at 2006-7-19 15:05:
Hehe, bagpipe has made a move again.
What does "defined" mean in the sentence "if not defined a (set a=%%a) else (set a=!a!;%%a)"? I've never seen this usage before. Can you explain this code...
It means that if a is not defined, then execute set a=%%a. If it is defined, then execute the statement after else!
|
|
2006-7-19 16:37 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
@echo off
setlocal EnableDelayedExpansion
set N=1
for /f "delims=" %%a in (原文件.txt) do (set New!N!=%%a
set /a N=!N! + 1)
set /a N=%N%-1
echo echo %%New1%%>Temp.bat
if %N% LEQ 1 goto End
set M=2
:len
for /f "delims=" %%a in (Temp.bat) do set X=%%a
echo %X% ;%%New%M%%%^>新文件.txt>Temp.bat
set /a M=%M%+1
if %M% GTR %N% goto End
goto len
:End
call Temp.bat
del Temp.bat
试过400行的没问题,500行就出错了,好像是for不下去了  ,根本未执行到Temp.bat,效率比bagpipe的差,不过SET设置变量没有bagpipe的大
Last edited by zxcv on 2006-7-19 at 18:54 ]
@echo off
setlocal EnableDelayedExpansion
set N=1
for /f "delims=" %%a in (original file.txt) do (set New!N!=%%a
set /a N=!N! + 1)
set /a N=%N%-1
echo echo %%New1%%>Temp.bat
if %N% LEQ 1 goto End
set M=2
:len
for /f "delims=" %%a in (Temp.bat) do set X=%%a
echo %X% ;%%New%M%%%^>new file.txt>Temp.bat
set /a M=%M%+1
if %M% GTR %N% goto End
goto len
:End
call Temp.bat
del Temp.bat
Tried with 400 lines and it was okay, but had an error with 500 lines. It seems like the for loop didn't continue. It didn't even execute to Temp.bat. The efficiency is worse than bagpipe's, but the SET variable setting isn't as large as bagpipe's
Last edited by zxcv on 2006-7-19 at 18:54 ]
|
|
2006-7-19 18:17 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
在批处理的实际应用中,建议不要使用太多的变量。
for /f "delims=" %i in (源文件.txt) do @set /p "var=%i;" <nul >>目标文件.txt
在命令提示符下运行。若要写到脚本中,更改%i为%%i。对于文本中出现引号的情况没有做出错处理。
In the practical application of batch processing, it is suggested not to use too many variables.
for /f "delims=" %i in (source file.txt) do @set /p "var=%i;" <nul >> target file.txt
Run it in the command prompt. If you want to write it into a script, change %i to %%i. There is no error handling for the situation where quotes appear in the text.
|
|
2006-7-19 22:39 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
还是3742668版主的厉害,我和bagpipe的500行就挂了,3742668版主的1200行还没问题
Still, the version of moderator 3742668 is powerful. The 500 lines of mine and bagpipe's hung up, but the 1200 lines of moderator 3742668 are still okay.
|
|
2006-7-19 22:52 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
没想到SET /P还能够连续对接文本内容,自愧不如..............
I didn't expect that SET /P can also continuously connect text content. I feel inferior..............
|
|
2006-7-20 09:05 |
|
|
amao
中级用户
  
积分 316
发帖 152
注册 2006-6-18
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
@sed -e :a -e "N;$!ba;s/ *\n/ ;/g;s/*$//" test.txt
参考了无奈何斑竹的有关代码加以修改,3600行以上也可以。
Last edited by amao on 2007-2-3 at 02:58 PM ]
@sed -e :a -e "N;$!ba;s/ *\n/ ;/g;s/*$//" test.txt
Referenced the relevant code of Bamboo Wu Nai He and made modifications, applicable for over 3600 lines.
Last edited by amao on 2007-2-3 at 02:58 PM ]
|
|
2007-2-3 14:17 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
set fso=createobject("scripting.filesystemobject")
set file=fso.opentextfile("a.txt")
do while file.atendofstream<>true
s=s & file.readline & " ;"
loop
file.close
set file=fso.createtextfile("b.txt")
file.write left(s,len(s)-1)
file.close
msgbox "操作成功",4096
```vbscript
set fso=createobject("scripting.filesystemobject")
set file=fso.opentextfile("a.txt")
do while file.atendofstream<>true
s=s & file.readline & " ;"
loop
file.close
set file=fso.createtextfile("b.txt")
file.write left(s,len(s)-1)
file.close
msgbox "Operation successful",4096
```
|
|
2007-2-4 01:18 |
|