Windows cmd.exe中是否有与“ cut -c”等效的文件? [英] Is there an equivalent to 'cut -c' in Windows cmd.exe?
问题描述
我有一些固定大小的文件,需要从中提取信息的固定字段大小的文件。通常,我会使用Cygwin( cut
等),但是在这种情况下,由于(硬骨头)我无法更改的管理策略,因此这不是一个选择。 必须使用Windows附带的标准XP工具集来完成。
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 has to be done using standard XP toolset included with Windows.
我需要提取偏移量为7的10个字符和偏移量为4的字符22(从零开始),然后将它们输出到文件中,但略有扭曲:
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:
- 第一个字段可能带有负号,正号,或没有符号(在开头或结尾)。该符号应移到最前面,如果是正号,则应完全删除。
- 第二个字段应删除前导和尾随空格。
例如:
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<
推荐答案
如果您使用的是现代窗户,则不受限制对于本地的cmd.exe命令,可以使用vbscript。如果您的政策也不是使用vbscript,那么我想您应该解雇您的管理人员:)
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 :)
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
基本上,要剪切字符串中的字符,请使用Mid()功能。请查看 vbscript文档查找更多信息。
Basically, to "cut" characters of a string, you use Mid() function. please look at the vbscript documentation to find out more.
将以上内容另存为test.vbs,然后在命令行上执行
Save the above as test.vbs and, on the command line, do
c:\test> cscript /nologo test.vbs > newfile
当然, substring也可以使用纯cmd.exe完成,但我将保留它
Of course, "substring" can also be done with pure cmd.exe but I will leave it to some others to guide you.
Pax更新:基于此答案,我提出了以下建议,这将是一个好的开始:
Update by Pax: Based on this answer, I came up with the following which will be a good start:
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.
结束更新。
这篇关于Windows cmd.exe中是否有与“ cut -c”等效的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!