在Windows cmd.exe(或其他XP标准工具)中有相当于'cut -c'吗? [英] Is there an equivalent to 'cut -c' in Windows cmd.exe (or other XP-standard tools)?

查看:148
本文介绍了在Windows cmd.exe(或其他XP标准工具)中有相当于'cut -c'吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些固定行大小的文件,固定字段大小,我需要从中提取信息。 Nornmally,我会使用Cygwin( cut 等),但这不是一个选项在这种情况下,由于(boneheaded)管理策略我不能改变。



我需要提取偏移7处的10个字符和偏移处的4个字符22(以零为基础),并将它们输出到文件,但略有扭曲:




  • 第一个字段可能为负, ,或没有符号(在开始或结束)。

  • 第二个字段应删除前导和尾随空格。



例如:

  1 2 3<文件,)
0123456789012345678901234567890123456789< - ignore(此处仅供参考)
xxxxxxx 15.22-yyyyyABCDzzzzzzzzzzz ...
xxxxxxx 122.00 + yyyyy XX zzzzzzzzzzz ...
xxxxxxx 9yyyyyYYY zzzzzzzzzzz ...

应产生(< 表示行结束):

  -15.22,ABCD< 
122.00,XX<
9,YYY<


解决方案

如果你使用现代化的窗户,到本地的cmd.exe命令,可以使用vbscript。如果你的策略不是使用vbscript,那么我想你应该解决你的管理:)

 设置objFS = CreateObject Scripting.FileSystemObject)
strFile =c:\test\file
设置objFile = objFS.OpenTextFile(strFile)
strFirstLine = objFile.ReadLine
Do Until objFile .AtEndOfStream
strLine = objFile.ReadLine
var1 = Mid(strLine,10)'从位置10开始执行子字符串
'var2 = Mid(strLine,< pos>,< length& )'获取下一个偏移量并保存到var2
WScript.Echo var1& var2'打印出来。
Loop

基本上,要剪切字符串的字符,功能。请查看 vbscript文档以了解更多信息。



将上述内容另存为test.vbs,然后在命令行中执行

  c:\test> cscript / nologo test.vbs> newfile 

当然,substring也可以用纯cmd.exe来完成,

根据这个答案,我想出了以下这将是一个好的开始:

 选项显式
dim objFs,objFile,strLine,value1,value2

if wscript.arguments.count< 1 then
wscript.echo用法:process< input-file>
wscript.quit
end if

set objFs = createObject(Scripting.FileSystemObject)
set objFile = objFs.openTextFile(wscript.arguments.item ))
do objFile.atEndOfStream
strLine = objFile.readLine
value1 = trim(mid(strLine,8,10))
value2 = trim ,4))
if right(value1,1)= - then value1 = - & left(value1,len(value1)-1)
如果right(value1,1)=+then value1 = left(value1,len(value1)-1) )=+then value1 = mid(value1,2)
wscript.echo value1& ,& value2
loop

这符合我们所有的要求。我们可以稍后将偏移量和长度转换为命令行参数。



结束更新。


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.

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:

  • 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<

解决方案

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

Basically, to "cut" characters of a string, you use Mid() function. please look at the vbscript documentation to find out more.

Save the above as test.vbs and, on the command line, do

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.

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.

End update.

这篇关于在Windows cmd.exe(或其他XP标准工具)中有相当于'cut -c'吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆