2013-01-17 134 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' 

编辑,包括在最后一行替换:)

相关问题