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 

请帮忙。

4

3 回答 3

0

You can use the following one liners if you have access to Linux/ Linux like environment (MinGW, Cygwin etc)

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

Edited to include substitution in last line :)

于 2013-08-22T16:46:19.303 回答
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
于 2013-01-17T15:08:51.423 回答
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 并将其写入另一个文件或同一个文件。

于 2013-01-17T15:13:36.323 回答