|
jinrich
初级用户
 
积分 26
发帖 11
注册 2006-12-15
状态 离线
|
『楼 主』:
for语句中怎么将%作为分隔符(已解决)
使用 LLM 解释/回答一下
@echo off
for /f "tokens=1 delims=% " %%a in (222.txt) do echo %%a>>2222.txt
pause
222.txt里的内容如下:
0|60|0|100.00%|0.00%|17.32%|14.57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
执行出错,看不懂……
起始我就是想把文件中的%去掉,用for执行不成功
不好意思,是我把自己的内容写的太不全引起大家的争执,<img src="images/smilies/face-sad.png" align="absmiddle" border="0">,只是为了说明问题,就没写的太详细,不好意
Last edited by jinrich on 2006-12-15 at 11:16 PM ]
@echo off
for /f "tokens=1 delims=%% " %%a in (222.txt) do echo %%a>>2222.txt
pause
The content in 222.txt is as follows:
0|60|0|100.00%|0.00%|17.32%|14.57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
There was an execution error, I don't understand...
At first, I just wanted to remove the % in the file, but the for execution didn't work
I'm sorry, it's because I didn't write my content in enough detail to cause disputes among everyone, :(, just to explain the problem, I didn't write it in too much detail, sorry
Last edited by jinrich on 2006-12-15 at 11:16 PM ]
|

Just does it;I lov bat |
|
2006-12-16 04:48 |
|
|
jinrich
初级用户
 
积分 26
发帖 11
注册 2006-12-15
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
难道%就做不成分隔符号,我在unix里也无法实现
Could the percent sign just not be used as a separator? I also can't make it work in Unix.
|

Just does it;I lov bat |
|
2006-12-16 05:14 |
|
|
tianzizhi
高级用户
   
积分 622
发帖 214
注册 2006-9-22
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
一个%不行,用两个%就行了,
@echo off
for /f "tokens=1 delims=%%" %%a in (222.txt) do echo %%a>>2222.txt
pause
One % doesn't work, two % are enough.
@echo off
for /f "tokens=1 delims=%%" %%a in (222.txt) do echo %%a>>2222.txt
pause
|
|
2006-12-16 05:26 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
除非你不想要第一个%后面的字符,否则代码不对。
The code is incorrect unless you don't want the characters after the first %.
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-16 05:39 |
|
|
a9319751
中级用户
  
积分 439
发帖 170
注册 2006-1-9
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
@echo off
for /f "tokens=1-6 delims=%%" %%a in (222.txt) do echo %%a%%b%%c%%d%%e%%f>>2222.txt
pause
@echo off
for /f "tokens=1-6 delims=%%" %%a in (222.txt) do echo %%a%%b%%c%%d%%e%%f>>2222.txt
pause
|

bat c c++ |
|
2006-12-16 05:52 |
|
|
tianzizhi
高级用户
   
积分 622
发帖 214
注册 2006-9-22
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
Originally posted by ccwan at 2006-12-16 05:39:
除非你不想要第一个%后面的字符,否则代码不对。
你没测试过不要乱下定论,谁说不对, 我想要后面的这样也对.
222.txt内容为:
0|60|0|100.00%|0.00%|17.32%|14.57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
@echo off
for /f "tokens=1 delims=%% " %%a in (222.txt) do echo %%a
pause
显示:0|60|0|100.00
@echo off
for /f "tokens=1,2 delims=%% " %%a in (222.txt) do echo %%a %%b
pause
显示:0|60|0|100.00 |0.00
@echo off
for /f "tokens=1,2,3,4,5,6 delims=%% " %%a in (222.txt) do echo %%a %%b %%c %%d %%e %%f
pause
显示:
0|60|0|100.00 |0.00 |17.32 |14.57 |55.64 |8.14|18.14|0.02|16.35|16.19|83.51|26
Originally posted by ccwan at 2006-12-16 05:39:
The code is incorrect unless you don't want the characters after the first %.
You didn't test it, don't make unfounded conclusions. Who said it's incorrect? I want the following, and this is also correct.
The content of 222.txt is:
0|60|0|100.00%|0.00%|17.32%|14.57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
@echo off
for /f "tokens=1 delims=%% " %%a in (222.txt) do echo %%a
pause
Displays: 0|60|0|100.00
@echo off
for /f "tokens=1,2 delims=%% " %%a in (222.txt) do echo %%a %%b
pause
Displays: 0|60|0|100.00 |0.00
@echo off
for /f "tokens=1,2,3,4,5,6 delims=%% " %%a in (222.txt) do echo %%a %%b %%c %%d %%e %%f
pause
Displays:
0|60|0|100.00 |0.00 |17.32 |14.57 |55.64 |8.14|18.14|0.02|16.35|16.19|83.51|26
|
|
2006-12-16 05:54 |
|
|
jinrich
初级用户
 
积分 26
发帖 11
注册 2006-12-15
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
谢谢达人,完成了
Thanks, expert. It's done.
|

Just does it;I lov bat |
|
2006-12-16 05:55 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
我不经过测试不会乱说的。
请试试下面的代码:
@echo off&setlocal enabledelayedexpansion
for /f %%a in (222.txt) do (
set str=%%a
set str=!str:%%=!
>>2222.txt echo !str!
)
pause
I don't talk nonsense without testing.
Please try the following code:
@echo off&setlocal enabledelayedexpansion
for /f %%a in (222.txt) do (
set str=%%a
set str=!str:%%=!
>>2222.txt echo !str!
)
pause
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-16 06:01 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
插一句,楼上的代码不够严谨,如果222.txt里面有空格或者水平制表符的话,for只能取到空格前面的字符。建议加上"delims="。
Just to interject, the code above is not rigorous enough. If there are spaces or horizontal tabs in 222.txt, the for loop will only get the characters before the space. It is recommended to add "delims=".
|
|
2006-12-16 06:41 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
谢谢lxmxn兄,当时只顾达到效果,确实考虑不周全,多谢提点!
Thank you, Brother lxmxn. At that time, I was only focused on achieving the effect and indeed didn't consider it comprehensively. Thanks for the reminder!
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-16 06:44 |
|
|
tianzizhi
高级用户
   
积分 622
发帖 214
注册 2006-9-22
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
@echo off&setlocal enabledelayedexpansion
for /f %%a in (222.txt) do (
set str=%%a
set str=!str:%%=!
>>2222.txt echo !str!
)
pause
我测试后结果为:
0|60|0|100.00|0.00|17.32|14.57|55.64|8.14|18.14|0.02|16.35|16.19|83.51|26
没什么错,我不知道你所谓的代码不对是什么,请你说清楚一下你说的会出现错的地方在哪里
@echo off&setlocal enabledelayedexpansion
for /f %%a in (222.txt) do (
set str=%%a
set str=!str:%%=!
>>2222.txt echo !str!
)
pause
The result of my test is:
0|60|0|100.00|0.00|17.32|14.57|55.64|8.14|18.14|0.02|16.35|16.19|83.51|26
There is nothing wrong. I don't know where the code you mentioned is wrong. Please clarify where the place you said would have an error is.
|
|
2006-12-16 06:44 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
Re tianzizhi :
把下面的内容作为222.txt的内容,再试试?
0|60|0|100.00%| 0.00%|17.32%|14 .57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
Re tianzizhi :
Make the following content as the content of 222.txt, and try again?
0|60|0|100.00%| 0.00%|17.32%|14 .57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
|
|
2006-12-16 06:47 |
|
|
tianzizhi
高级用户
   
积分 622
发帖 214
注册 2006-9-22
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Originally posted by lxmxn at 2006-12-16 06:47:
Re tianzizhi :
把下面的内容作为222.txt的内容,再试试?
0|60|0|100.00%| 0.00%|17.32%|14 .57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
...
lxmxn,你可能误会了,我讲的是ccwan
说我三楼回答在某些地方会错,
『第 3 楼』:
一个%不行,用两个%就行了,
@echo off
for /f "tokens=1 delims=%%" %%a in (222.txt) do echo %%a>>2222.txt
pause
『第 4 楼』:
除非你不想要第一个%后面的字符,否则代码不对。
我不清楚他说的代码不对是指什么,然后他贴出你看的那个代码,我又测试一下,没什么错,
对于你就指的222.txt内容里面有空格当然要加delims=了,
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (222.txt) do (
set str=%%a
set str=!str:%%=!
>>2222.txt echo !str!
)
pause
测试结果为:
0|60|0|100.00| 0.00|17.32|14 .57|55.64|8.14|18.14|0.02|16.35|16.19|83.51|26
也没什么错啊,还是请ccwan就一下他所说的错是什么,我想知道.
Originally posted by lxmxn at 2006-12-16 06:47:
Re tianzizhi :
Take the following content as the content of 222.txt and try again?
0|60|0|100.00%| 0.00%|17.32%|14 .57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
...
lxmxn, you may have misunderstood. What I'm talking about is ccwan.
Said that my answer on the third floor would be wrong in some places,
『Floor 3』:
One % is not okay, use two %s.
@echo off
for /f "tokens=1 delims=%%" %%a in (222.txt) do echo %%a>>2222.txt
pause
『Floor 4』:
Unless you don't want the characters after the first %, the code is incorrect.
I don't know what he means by the code being incorrect, and then he posted the code you saw. I tested it again, and there's nothing wrong.
For the content in 222.txt that you mentioned, there are spaces, so you need to add delims=.
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (222.txt) do (
set str=%%a
set str=!str:%%=!
>>2222.txt echo !str!
)
pause
The test result is:
0|60|0|100.00| 0.00|17.32|14 .57|55.64|8.14|18.14|0.02|16.35|16.19|83.51|26
There's nothing wrong either. Still, please let ccwan clarify what he means by the mistake. I want to know.
|
|
2006-12-16 06:59 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
呵呵,
222.txt内容为:
0|60|0|100.00%|0.00%|17.32%|14.57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
@echo off
for /f "tokens=1 delims=%% " %%a in (222.txt) do echo %%a
pause
显示:0|60|0|100.00
结果并非楼主所要,当然不对了,当时情况就是那样啊!
Hehe,
The content of 222.txt is:
0|60|0|100.00%|0.00%|17.32%|14.57%|55.64%|8.14|18.14|0.02|16.35|16.19|83.51|26%
@echo off
for /f "tokens=1 delims=%% " %%a in (222.txt) do echo %%a
pause
Displays: 0|60|0|100.00
The result is not what the original poster wanted, of course it's incorrect. The situation at that time was just like that!
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-16 07:06 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
哦,误会了。不好意思。
Oh, it was a misunderstanding. Sorry about that.
|
|
2006-12-16 07:07 |
|