2014-01-21 52 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 

這裏的技巧是,你讀當前行之前打印從以前的迭代線。這樣最後一行被省略了。