|
tangz
新手上路

积分 8
发帖 2
注册 2006-5-29
状态 离线
|
『楼 主』:
求助,如何删除变量里的指定的字符
使用 LLM 解释/回答一下
我用for /f从外部文件读取数据,%%j的值为00-e0-4c-88-88-ee。想去掉其中的-,修改%%j的值为00e04c8888ee。请为该如何写脚本?谢谢。
To remove the hyphens from `%%j` which has the value `00-e0-4c-88-88-ee` and change it to `00e04c8888ee` in a batch script using `for /f`, you can use the following approach. Here's a sample script snippet:
```batch
@echo off
setlocal enabledelayedexpansion
set "input=00-e0-4c-88-88-ee"
set "output="
for /f "tokens=1-6 delims=-" %%a in ("%input%") do (
set "output=%%a%%b%%c%%d%%e%%f"
)
echo %output%
endlocal
```
In the above script:
1. First, we enable delayed expansion with `enabledelayedexpansion`.
2. We define the input string with the hyphens.
3. Then, we use `for /f` with `delims=-` to split the input string by hyphens into tokens `%%a` to `%%f`.
4. We concatenate these tokens together to form the string without hyphens, storing the result in `output`.
5. Finally, we echo the resulting string which should be `00e04c8888ee`.
If you are already reading from an external file using `for /f`, you can adjust the script accordingly to process each line from the file similarly. For example, if you have a file named `data.txt` with each line being the string with hyphens, the script would be:
```batch
@echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%i in ("data.txt") do (
set "line=%%i"
set "result="
for /f "tokens=1-6 delims=-" %%a in ("!line!") do (
set "result=%%a%%b%%c%%d%%e%%f"
)
echo !result!
)
endlocal
```
This script reads each line from `data.txt`, processes it to remove hyphens, and then echoes the result.
|
|
2006-11-8 03:13 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
删除变量中指定的字符就相当于用一个“空”字符即""来替换掉指定内容~:)
如何来替换文本中的字符
http://www.cn-dos.net/forum/viewthread.php?tid=22245&fpage=1&highlight=%E2%80%9C
详见贴子第五楼(你照抄并把字串改成你需要的就行了)较为通用的方法:)
如果想各种字串与变量应用的方法都熟悉一下,见下面贴子中的索引:
http://www.cn-dos.net/forum/viewthread.php?tid=24549&fpage=1
Last edited by redtek on 2006-11-8 at 04:08 AM ]
Deleting specified characters from a variable is equivalent to replacing the specified content with an "empty" character, which is "~:)"
How to replace characters in text
http://www.cn-dos.net/forum/viewthread.php?tid=22245&fpage=1&highlight=%E2%80%9C
For details, see the fifth floor of the post (you can copy it directly and change the string to what you need). A more general method ~:)
If you want to familiarize yourself with various string and variable application methods, see the index in the following post:
http://www.cn-dos.net/forum/viewthread.php?tid=24549&fpage=1
Last edited by redtek on 2006-11-8 at 04:08 AM ]
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-8 04:06 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-11-8 04:51 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
|
2006-11-8 05:00 |
|
|
zerostudy
中级用户
  
积分 266
发帖 98
注册 2006-4-21
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
楼上的讲讲为什么%a:-=%可以除去全部-啊?可以说说用法吗?
The person upstairs, please explain why %a:-=% can remove all "-"? Can you talk about the usage?
|

纵是千年万年 亦难以忘记 |
|
2006-11-8 05:22 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
%a:x=y%的作用是:将变量 "a" 中包含的所有 "x" 都换成 "y" ,而上例中等号(=)后面没有字符,就是替换成空值(不是空格),这就相当于将 "-" 删除了。
The function of %a:x=y% is: replace all "x" contained in variable "a" with "y". In the above example, there are no characters after the equal sign (=), which is replaced with an empty value (not a space), which is equivalent to deleting the "-".
|
|
2006-11-8 05:51 |
|
|
zerocq
中级用户
  
积分 458
发帖 196
注册 2006-10-5
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
for /f里的delims=添加一个-号
然后%%j就变成%%j-%%k-%%l-%%m
00e04c8888ee就是%%j%%k%%l%%m
Add a hyphen in delims= in for /f
Then %%j becomes %%j-%%k-%%l-%%m
00e04c8888ee is %%j%%k%%l%%m
|
|
2006-11-8 07:10 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
真是人多力量大:)zerocq兄又是一种方法,哈哈……
可惜俺今天的分儿全都都加完了:(
It's really that many hands make light work: ) Brother zerocq has another method, haha...
It's a pity that all my points have been used up today: (
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-8 07:23 |
|
|
honhaizh
初级用户
 
积分 35
发帖 18
注册 2006-4-26
状态 离线
|
|
2006-11-16 20:32 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
文件中的替换我喜欢用sed
I like to use sed for replacements in files
|
|
2006-11-17 01:40 |
|
|
hxuan999
中级用户
   DOS之日
积分 337
发帖 161
注册 2006-11-4
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
我想CMD也就只有这两个方法了吧.高手们谁还可以想出来第三种呀,不用第三方工具的.
I think there are only these two methods for CMD. Who among the experts can come up with a third one, without using third-party tools?
|

for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul |
|
2006-11-23 02:54 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
Too bad I've already used up all my points today: (
I'll help you add some
|

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>" |
|
2006-11-23 07:03 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
哈哈……怎么加分选择只能是 1、3、5、7、9……原来的+2分的选项怎么没了?
Haha... Why are the only plus points options 1, 3, 5, 7, 9... Where did the original +2 points option go?
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-23 07:08 |
|