2013-01-17 44 views
1

我在一些文件夾中有超過5k的文本文件。現在我只需要從每個文本文件的第一行和最後一行刪除"-"如何編寫腳本以僅搜索文本中的第一行和最後一行,並從那裏刪除「 - 」?

我不知道該怎麼做,有沒有人可以幫忙?我應該用什麼來做到這一點? VBS或正常批次

操作系統:Windows7的

文本文件是這樣的:01.txt


% 01-A247M15 G70 

N0001 G30 G17 X-100 Y-100 Z0 

N0002 G31 G90 X100 Y100 Z45 

N0003 ; --PART NO.: NC-HON.PHX01.COVER-SHOE.DET-1000.050 

N0004 ; --TOOL: 8.55 X .3937 

N0005 D00 Q50 P01 +8.5500 ; TOOL DIA 

N0006 D00 Q51 P01 +0.3920 ; TOOL RAD 

N0007 % 01-A247M15 G70 

請幫助。

+0

是包括線在文件中? – Default

回答

0

使用VBScript它更簡單...

首先你解析文本文件到一個數組:http://www.windowsitpro.com/content/content/46489/Listing_01.txt

Function FileToArray(ByVal strFile, ByVal blnUNICODE) 
    Const FOR_READING = 1 
    Dim objFSO, objTS, strContents 

' BEGIN CALLOUT A 
    FileToArray = Split("") 
' END CALLOUT A 

    Set objFSO = CreateObject("Scripting.FileSystemObject") 

    If objFSO.FileExists(strFile) Then 
    On Error Resume Next 
    Set objTS = objFSO.OpenTextFile(strFile, FOR_READING, False, blnUNICODE) 
    If Err = 0 Then 
     strContents = objTS.ReadAll 
     objTS.Close 
' BEGIN CALLOUT B 
     FileToArray = Split(strContents, vbNewLine) 
' END CALLOUT B 
    End If 
    End If 
End Function 

那麼你是一個對REPLACE將數組的第一行和最後一行寫入另一個文件或同一個文件。

+0

謝謝你的幫助。 – Blitzcrank

0

你還沒有指出你想要哪個腳本perl,unix。所以,我給你的算法,

1. read first line 
2. Split with - as delimiter (cut in unix) 
3. paste the split words in redirect to new file >> newfile 
4. get count of lines of file (wc in unix) 
5. get the 2nd line till last one but line and append to new file (head and tail in unix) 
6. do the same steps 1, 2, and 3 for last line and append to new file 
+0

從「我應該用什麼來做到這一點?vbs或正常批次」這個問題。 VBS == VBScript –

0

您可以使用下面的一個襯墊,如果你有這樣的環境進入到Linux/Linux操作系統(MinGW的不同,Cygwin等)

find ./ -name *.txt | xargs sed -i '1s/-//g' 
    find ./ -name *.txt | xargs sed -i '$s/-//g' 

編輯,包括在最後一行替換:)

相關問題