200多万行数的数据,经过筛选后117万多行,win10实测,正则匹配同时包含多个字符串的行没有for循环来的快。
思路同8楼,改成for循环。- $file1 = 'E:\Test\大藏经解析\IDS.txt';
- $file2 = 'E:\Test\大藏经解析\dump.txt';
- $file3 = 'E:\Test\大藏经解析\a.txt';
- $file4 = 'E:\Test\大藏经解析\b.txt';
-
- #遍历 IDS.txt 每一行,加入字典
- $Dic1 = New-Object 'Collections.Generic.Dictionary[string, string]';
- $Dic3 = New-Object 'Collections.Generic.Dictionary[string, string]';
- $arr = [IO.File]::ReadAllLines($file1) -match '^u';
- $count = $arr.Count;
- for( $i=0; $i -lt $count; $i++ ){
- $a = $arr[$i].Trim().Split("`t ", 2, 'RemoveEmptyEntries'); #分割成2列
- if( !$Dic1.ContainsKey($a[1]) ){
- $Dic1.Add($a[1], $a[0]); #Dic1:key=第2列中文字符,value=第1列unicode码
- }
- if( !$Dic3.ContainsKey($a[0]) ){
- $Dic3.Add($a[0], $a[1]); #Dic3:key=第1列unicode码,value=第2列中文字符
- }
- }
-
- #遍历 dump.txt 每一行,加入字典
- $Dic2 = New-Object 'Collections.Generic.Dictionary[string, string]';
- $arr = [IO.File]::ReadAllLines($file2) -match '^u[0-9a-f]+\s+.*u[0-9a-f]+' -notMatch '^u3013\s'; #去掉u3013开头的行
- $count = $arr.Count;
- for( $i=0; $i -lt $count; $i++ ){
- $a = $arr[$i].Trim().Split("`t ", 2, 'RemoveEmptyEntries'); #分割成2列
- $k = forEach( $m In [regex]::Matches($a[1], '(?i)u[0-9a-f]+') ){
- $m.Groups[0].Value;
- }
- $key = $k -join ' ';
- if( !$Dic2.ContainsKey($key) ){
- $Dic2.Add($key, $a[0]); #Dic2:key=第2列的多个unicode码,value=第1列的unicode码
- }
- }
-
- #遍历 a.txt 每一行,如果在一行中同时匹配多个unicode码,写入b.txt
- $fsw = New-Object System.IO.StreamWriter($file4, $false, [Text.Encoding]::UTF8);
- $arr = [IO.File]::ReadAllLines($file3) -match '^CB';
- $count = $arr.Count;
- for( $i=0; $i -lt $count; $i++ ){
- [char[]]$chs = $arr[$i].Split('[]')[1]; #a.txt每一行的中文字符
- [Collections.ArrayList]$a = @(); #数组a,存放中文字符对应的unicode码
- for( $j=0; $j -lt $chs.Count; $j++ ){
- $s = '' + $chs[$j]; #char转string
- if( $Dic1.ContainsKey($s) ){
- [void]$a.Add( $Dic1[$s] );
- } else { break; }
- }
- if( $a.Count -ne $chs.Count ){ continue; }
- $h = @{}; #哈希表,存放Dump.txt的第一列unicode码
- forEach( $key In $Dic2.Keys ){
- if( $h.ContainsKey($Dic2[$key]) ){ continue; }
- $flag = $true;
- for( $j=0; $j -lt $a.Count; $j++ ){
- if( $key.indexOf($a[$j]) -lt 0 ){
- $flag = $false;
- break;
- }
- }
- if( $flag ){ $h[$Dic2[$key]] = $true; }
- }
- if( $h.Count -eq 0 ){ continue; }
- $s = $h.Keys -join "`t";
- $s = [regex]::Replace($s, '\S+', {param($m); $m.Value + '-' + $Dic3[$m.Value]});
- echo( $arr[$i] + "`t" + $s );
- $fsw.WriteLine( $arr[$i] + "`t" + $s );
- $fsw.Flush();
- }
-
- $fsw.Dispose();
- echo 'Done';
- [Console]::ReadKey();
复制代码
|