2014-01-21 51 views
1

如果我的术语不正确,请提供任何帮助并请谅解。用于删除CSV文件中的行的等效sed命令

find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';' 

这unix命令,在Cygwin上运行,将通过在目录下,以.CSV
为每个最终文件,它删除了一6行和最后一行,这就是返回的所有文件和子目录文件。

我的问题是我如何在vbs脚本中编写等价物?

我尝试编辑这样的事情(taken from here),以适应我的要求,但没有喜悦。

Set fso = CreateObject("Scripting.FileSystemObject") 
Set csv = fso.OpenTextFile(WScript.Arguments(...)) 

If Not csv.AtEndOfStream Then csv.SkipLine 'skip first row 

Do Until csv.AtEndOfStream 
    line = csv.ReadLine 
    'process read line 
Loop 

csv.Close 

注:那么我想定期调度此vbs脚本使用任务调度

编辑 文件看起来运行脚本之前如下:(ROW7和行之间的行数?是可变的和未知的)

<blank line/row1> 
<blank line/row2> 
<Text would be on this row/line3> 
<Text would be on this row/line4> 
<Text would be on this row/line5> 
<blank line/row6> 
<Text would be on this row/line7> 
. 
. 
. 
<Text would be on this row/line??> 
<Text would be on this Last row/line> 
+0

+1试图解决您的问题。但是...... sed和vbs之间的文本处理操作模式有很少的共同点。考虑编辑您的问题,以包含示例数据,以进一步显示来自每个'sed'命令的文本中发生的变化的更多示例。一些'vbs'程序员可能会理解'sed'命令,但是更多的人会理解并且示例说我有这个数据X,并且我需要将它转换为Y.祝你好运。 – shellter

回答

0
dim fso, csv, Iter, ThisLine 

Set fso = CreateObject("Scripting.FileSystemObject") 
Set csv = fso.OpenTextFile(WScript.Arguments(...)) 

Iter=1 
while not csv.AtEndOfStream 
    ' Buffer the line for last line test 
    ThisLine = csv.ReadLine 

    ' stdout if after the 6th line and not the last (the one EOF is true after reading the line) 
    if (Iter > 6) and (not csv.AtEndOfStream) then 
     wscript.echo ThisLine 
    end if 
    Iter = Iter + 1 
wend 

csv.Close 

如果约束是X最后一行,而不是最后一个,你应该先读文件,计算比巴线路用此对象评估行号(流文本文件)。 否则,如果不是太大,请在内存中加载X行的缓冲区或使用另一种方法读取文件(例如,在ADO中有一个光标的概念以知道行的绝对位置和最后一行数)

+0

这看起来像一个起点。我只需要弄清楚如何将它写入文件。 – HattrickNZ

0

我会做这样的:

Set fso = CreateObject("Scripting.FileSystemObject") 

filename = "C:\path\to\your.csv" 

Set inFile = fso.OpenTextFile(filename) 
Set outFile = fso.OpenTextFile(filename & ".tmp", 2, True) 

Do Until inFile.AtEndOfStream 
    If inFile.Line <= 6 Then 
    inFile.SkipLine 
    Else 
    if Not IsEmpty(line) Then outFile.WriteLine line 
    line = inFile.ReadLine 
    End If 
Loop 

inFile.Close 
outFile.Close 

fso.DeleteFile filename, True 
fso.MoveFile filename & ".tmp", filename 

这里的技巧是,你读当前行之前打印从以前的迭代线。这样最后一行被省略了。