[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
只根据括号内容(.*?)区分就很简单
输入0.csv  输出1.csv
  1. #&cls&@ cd /d "%~dp0" & powershell -c "Get-Content '%~0' | Out-String | Invoke-Expression" & pause&exit
  2. $set = @{}
  3. $content = Get-Content '0.csv' -Encoding UTF8
  4. $content | foreach {
  5.     $arr = $_ -split ','
  6.     $a = ($arr[2] -replace '\(.*?\)','').Trim() + ',' + ($arr[3] -replace '\(.*?\)','').Trim()
  7. if($set.$a -eq $null){
  8. $set.$a = $_
  9. } else {
  10. $set.$a = $set.$a + "`n" + $_
  11. }
  12. }
  13. $set.Values | Where-Object { ($_ -split '\n').Count -gt 1 } | Out-File '1.csv'
复制代码

TOP

回复 4# 5i365


    没有测试大量数据
  1. cls
  2. #函数 获取两个字符串最大相同字符个数
  3. function Get-SameNum([string]$str1,[string]$str2){
  4.     $max = 0
  5.     $len = 0
  6.     if($str1.Length -le $str2.Length){
  7.         $len = $str1.Length
  8.         $s1 = $str1
  9.         $s2 = $str2
  10.     } else {
  11.         $len = $str2.Length
  12.         $s1 = $str2
  13.         $s2 = $str1
  14.     }
  15.     for($i = 0;$i -lt $len; $i++){
  16.         for($j = 1; $j -lt $len-$i+1; $j++){
  17.             $s_t = $s1.Substring($i,$j)
  18.             if($s2.IndexOf($s_t) -ne -1){
  19.                 if($s_t.Length -gt $max){
  20.                     $max = $s_t.Length
  21.                 }
  22.             }
  23.         }
  24.     }
  25.     return $max
  26. }
  27. #阈值设置 >=
  28. $m1 = 3
  29. $m2 = 3
  30. #比较并输出
  31. $content = Get-Content '0.csv' -Encoding UTF8
  32. &{
  33.     $content | foreach {
  34.         $__ = $_
  35.         $content | Where-Object { $_ -ne $__} | foreach {
  36.             $arr1 = $__ -split ','
  37.             $arr2 = $_ -split ','
  38.             $a = Get-SameNum -str1 $arr1[2] -str2 $arr2[2]
  39.             $b = Get-SameNum -str1 $arr1[3] -str2 $arr2[3]
  40.             if($a -ge $m1 -and $b -ge $m2){
  41.                 $__
  42.             }
  43.         }
  44.     }
  45. } | Out-File '1.csv'
复制代码
1

评分人数

    • 5i365: 感谢,测试可用技术 + 1

TOP

回复 7# 5i365


    可以传csv附件给我测试优化

TOP

本帖最后由 went 于 2022-1-8 13:09 编辑

回复 11# 5i365
还是 输入0.csv 输出1.csv
  1. #&cls&@powershell -c "Get-Content '%~0' | Select-Object -Skip 1 | Out-String | Invoke-Expression" &pause&exit
  2. cls
  3. #函数 获取两个字符串最大相同字符个数
  4. function Get-SameNum([string]$str1,[string]$str2){
  5.     $max = $len = 0
  6.     if($str1.Length -le $str2.Length){
  7.         $len = $str1.Length; $s1 = $str1; $s2 = $str2
  8.     } else {
  9.         $len = $str2.Length; $s1 = $str2; $s2 = $str1
  10.     }
  11.     for($i = 0;$i -lt $len; $i++){
  12.        if($len-$i -le $max){ break; }
  13.         for($j = 1; $j -lt $len-$i+1; $j++){
  14.             $s_t = $s1.Substring($i,$j)
  15.             if($s2.IndexOf($s_t) -ne -1){
  16.                 if($s_t.Length -gt $max){ $max = $s_t.Length }
  17.             }
  18.         }
  19.     }
  20.     return $max
  21. }
  22. #阈值设置 >=
  23. $m1 = 3
  24. $m2 = 3
  25. #比较并输出
  26. $list = [System.Collections.ArrayList](Get-Content '0.csv' -Encoding UTF8 | Select-Object -Skip 1)
  27. &{
  28.     &{
  29.         $list | Sort-Object {
  30.             $arr = $_ -split ','
  31.             ($arr[2] -replace '[\(\[\{].*[\)\]\}]',''),
  32.             ($arr[3] -replace '[\(\[\{].*[\)\]\}]',''),
  33.             ($arr[0])
  34.         } | foreach {
  35.             $line = $_
  36.             $singer = ($_ -split ',')[2]
  37.             $song = ($_ -split ',')[3]
  38.             if($last_line -ne $null){
  39.                 $a = Get-SameNum -str1 $singer -str2 $last_singer
  40.                 if($a -ge $m1){
  41.                     $b = Get-SameNum -str1 $song -str2 $last_song
  42.                     if($b -ge $m2){
  43.                         if(!$last){ $last_line; $last = $true}
  44.                         $_
  45.                     }
  46.                 } else {
  47.                     $last = $false
  48.                 }
  49.             }
  50.             $last_line = $line
  51.             $last_singer = $singer
  52.             $last_song = $song
  53.         }
  54.     }
  55. } | Out-File '1.csv'
复制代码

TOP

回复 13# 5i365


    37.38行修改下
  1. $singer = ($_ -split ',')[2] -replace '[\(\[\{].*[\)\]\}]',''
  2. $song = ($_ -split ',')[3] -replace '[\(\[\{].*[\)\]\}]',''
复制代码
1

评分人数

    • 5i365: 实测上万条数据也就弹指一挥间完成, 高效!技术 + 1

TOP

返回列表