jchang 发表于 2010-4-29 11:13:27

如何删除文本中以指定数据开头的重复行

文本如下:
(OCC)COMBINATION #1                                                         
10    2100   1684   6829       9691   9853   2136   Rigid ANC   
(OCC)COMBINATION #1   
130       0      0   2740          0      0      0   Rigid +Z   
(OCC)COMBINATION #2      
130       0   2964      0          0      0      0   Rigid GUI
(OCC)COMBINATION #2   
130       0      0   2740          0      0      0   Rigid +Z      

要删除以(OCC)开头的重复行,变成:
(OCC)COMBINATION #1                                                         
10    2100   1684   6829       9691   9853   2136   Rigid ANC   
130       0      0   2740          0      0      0   Rigid +Z   
(OCC)COMBINATION #2      
130       0   2964      0          0      0      0   Rigid GUI
130       0      0   2740          0      0      0   Rigid +Z   

请教脚本应该如何考虑呢?

水木子 发表于 2010-4-29 11:21:19

hehe !楼主又来一道正则题啊!

newuser 发表于 2010-4-29 11:31:29

回复 1# jchang
正则表达式真的很难,看的头都大了,还是处于混沌之中!!!

jchang 发表于 2010-4-29 11:53:51

呵呵,最近遇到的正则问题比较多

3mile 发表于 2010-4-29 14:45:55

字符串内容:(OCC)COMBINATION #1                                                         
10    2100   1684   6829       9691   9853   2136   Rigid ANC   
(OCC)COMBINATION #2   
130       2100      223   2740          2740      0      0   Rigid +Z   
(OCC)COMBINATION #2   
130       2600      223   2740          2740      0      0   Rigid +Z   
(OCC)COMBINATION #1   
130       0      0   2740          0      0      0   Rigid +Z   
(OCC)COMBINATION #2      
130       0   2964      0          0      0      0   Rigid GUI
(OCC)COMBINATION #2   
130       0      0   2740          0      0      0   Rigid +Z   #include <array.au3>
If FileExists("test.txt") Then FileDelete("test.txt")
$str=FileRead("1.txt");1.txt文件内容为楼主提供字符串
$arr=StringRegExp($str,'\([^()]*?\).+\n.+',3)
_ArraySort($arr)
;_ArrayDisplay($arr)
For $i=0 To UBound($arr)-1
        $str1=StringStripWS($arr[$i],4)
        $str2=StringReplace($str1,21,"=")       
        $arr2=StringSplit($str2,"=")       
        IniWrite("test.txt",$arr2,$i,$arr2)       
Next
MsgBox(0,0,StringRegExpReplace(FileRead("test.txt"),'\d=',''))

水木子 发表于 2010-4-29 15:14:38

字符串内容:
3mile 发表于 2010-4-29 14:45 http://www.autoitx.com/images/common/back.gif
呵呵!居然用ini写回去,方法不错!学习了。
不过这样一来输出结果和楼主的要求有点出入,还需要再次整理下吧?

afan 发表于 2010-4-29 16:08:07

本帖最后由 afan 于 2010-4-29 16:11 编辑

将1楼测试字符串保存为 'test.txt'Local $file = FileOpen('test.txt'), $line, $_line, $str
While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        $_line = StringRegExp($line, '^(\(OCC\).+?)\h+$', 3)
        If Not @error Then
                If Not IsDeclared('_' & $_line) Then
                        Assign('_' & $_line, 0)
                        $str &= $line & @CRLF
                EndIf
        Else
                $str &= $line & @CRLF
        EndIf
WEnd
FileDelete('test_new.txt')
FileWrite('test_new.txt', $str)
ShellExecute('test_new.txt')

jchang 发表于 2010-4-30 22:46:15

多谢各位,该问题解决啦
页: [1]
查看完整版本: 如何删除文本中以指定数据开头的重复行