Windows cmd.exe 中是否有相当于“cut -c”的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1564527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an equivalent to 'cut -c' in Windows cmd.exe?
提问by paxdiablo
I have some files of fixed line size, fixed field size that I need to extract information from. Nornmally, I'd use Cygwin (cut
et al), but that's not an option in this case due to (boneheaded) management policies I can't change. It hasto be done using standard XP toolset included with Windows.
我有一些固定行大小、固定字段大小的文件,我需要从中提取信息。通常,我会使用 Cygwin(cut
等人),但在这种情况下这不是一个选项,因为我无法更改(愚蠢的)管理策略。它必须使用 Windows 附带的标准 XP 工具集来完成。
I need to extract the 10 characters at offset 7 and 4 characters at offset 22 (zero-based), and output them to a file but with a slight twist:
我需要提取偏移量 7 处的 10 个字符和偏移量 22 处的 4 个字符(从零开始),然后将它们输出到文件中,但稍有改动:
- The first field may have a negative, positive, or no sign (at the start or end). The sign should be moved to the front, or removed totally if it's positive.
- The second field should have leading and trailing spaces removed.
- 第一个字段可能有负号、正号或没有符号(在开始或结束处)。标志应该移到前面,如果是正面的,则应将其完全移除。
- 第二个字段应删除前导和尾随空格。
For example:
例如:
1 2 3 <- ignore (these lines not in file,)
0123456789012345678901234567890123456789 <- ignore ( here only for info.)
xxxxxxx 15.22-yyyyyABCDzzzzzzzzzzz...
xxxxxxx 122.00+yyyyy XX zzzzzzzzzzz...
xxxxxxx 9yyyyyYYY zzzzzzzzzzz...
should produce (<
indicates end of line):
应该产生(<
表示行尾):
-15.22,ABCD<
122.00,XX<
9,YYY<
采纳答案by ghostdog74
If you working with modern windows, you are not restricted to cmd.exe commands natively, you can use vbscript. If your policy is not to use vbscript either, then I guess you should sack your management :)
如果您使用现代 Windows,则本机不受 cmd.exe 命令的限制,您可以使用 vbscript。如果您的政策也不是使用 vbscript,那么我想您应该解雇您的管理层 :)
Set objFS=CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
Set objFile = objFS.OpenTextFile(strFile)
strFirstLine = objFile.ReadLine
Do Until objFile.AtEndOfStream
strLine= objFile.ReadLine
var1 = Mid(strLine,10) ' do substring from position 10 onwards
' var2 = Mid (strLine,<pos>,<length>) ' get next offset and save to var2
WScript.Echo var1 & var2 ' print them out.
Loop
Basically, to "cut" characters of a string, you use Mid() function. please look at the vbscript documentationto find out more.
基本上,要“剪切”字符串的字符,请使用 Mid() 函数。请查看vbscript 文档以了解更多信息。
Save the above as test.vbs and, on the command line, do
将上述内容另存为 test.vbs 并在命令行中执行
c:\test> cscript /nologo test.vbs > newfile
Of course, "substring" can also be done with pure cmd.exe but I will leave it to some others to guide you.
当然,“子串”也可以用纯 cmd.exe 来完成,但我会让其他人来指导你。
Update by Pax:Based on this answer, I came up with the following which will be a good start:
Pax 更新:基于此答案,我提出了以下建议,这将是一个良好的开端:
option explicit
dim objFs, objFile, strLine, value1, value2
if wscript.arguments.count < 1 then
wscript.echo "Usage: process <input-file>"
wscript.quit
end if
set objFs=createObject("Scripting.FileSystemObject")
set objFile = objFs.openTextFile(wscript.arguments.item(0))
do until objFile.atEndOfStream
strLine= objFile.readLine
value1 = trim(mid(strLine, 8, 10))
value2 = trim(mid(strLine, 23, 4))
if right(value1,1) = "-" then value1 = "-" & left(value1,len(value1)-1)
if right(value1,1) = "+" then value1 = left(value1,len(value1)-1)
if left(value1,1) = "+" then value1 = mid(value1,2)
wscript.echo value1 & "," & value2
loop
This matches all the requirements we had. We can make the offsets and lengths into command-line arguments later.
这符合我们的所有要求。我们可以稍后将偏移量和长度作为命令行参数。
End update.
结束更新。
回答by Daren Thomas
This site has some pointers on how to extract substrings in cmd.exe: http://www.dostips.com/DtTipsStringManipulation.php
这个站点有一些关于如何在 cmd.exe 中提取子字符串的提示:http: //www.dostips.com/DtTipsStringManipulation.php
That site suggests that you can use
该网站建议您可以使用
%varname:~2,3%
to subscript a variable. This seems to fill your needs, except you now have to get each line into a variable.
为变量添加下标。这似乎满足了您的需求,但您现在必须将每一行都放入一个变量中。
Next you want to look at the ghastly for
loop syntax and if
and branching (you can goto :labels
in batch).
接下来,您要查看可怕的for
循环语法if
和分支(您可以:labels
批量转到)。
This stuff is all rather ugly, but if you really have to go there...
这些东西都相当丑陋,但如果你真的必须去那里......
Here is a page in SO on looping through files and doing stuff to them: How do you loop through each line in a text file using a windows batch file?
这是 SO 中的一个关于循环遍历文件并对它们执行操作的页面:如何使用 Windows 批处理文件循环遍历文本文件中的每一行?
回答by Lumi
Here's a small script (needs to be in a BAT/CMD file) expanding on what Daren Thomas suggested:
这是一个小脚本(需要在 BAT/CMD 文件中)扩展了 Daren Thomas 的建议:
@echo off
setlocal
set key=HKCU\Software\Microsoft\Windows\CurrentVersion\Ext\Stats\{D27CDB6E-AE6D-11CF-96B8-444553540000}\iexplore\AllowedDomains
echo.
echo Liste der fuer Flash im IE zugelassenen Domaenen:
echo =================================================
for /f "usebackq tokens=11 delims=\" %%l in (`call reg query "%key%" /s`) do echo. %%l
echo.
endlocal
The FOR loop is the central part. Note the use of command modifiers in double quotes. I specify tokens=11
because I'm only interested in the subkeys of AllowedDomains
, which is at position 10.
FOR 循环是核心部分。请注意在双引号中使用命令修饰符。我指定tokens=11
是因为我只对AllowedDomains
位置 10 的的子键感兴趣。
Be sure to read the help in for /?
. Daren is right when he says this stuff is all rather ugly. And it easily breaks down on modification. There's a lot of non-intuitive subtleties with cmd.exe
script syntax.
请务必阅读 中的帮助for /?
。达仁说这些东西都很丑是对的。它很容易在修改时崩溃。cmd.exe
脚本语法有很多不直观的微妙之处。
By the way, the GUID is the COM class ID for the Shockwave Flash Add-on. It's been around since at least 2001so might well continue to be relevant for the foreseeable future. The purpose of the script is to list the domains where Flash, which I prefer to block by default, has been allowed.
顺便说一下,GUID 是 Shockwave Flash Add-on 的 COM 类 ID。它至少从 2001 年就已经存在,因此在可预见的未来可能会继续保持相关性。该脚本的目的是列出允许我默认阻止 Flash的域。