这个 powershell "比对”命令能优化吗? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
1054850490
V2EX    PowerShell

这个 powershell "比对”命令能优化吗?

  •  
  •   1054850490 2023-02-17 08:28:11 +08:00 2372 次点击
    这是一个创建于 970 天前的主题,其中的信息可能已经有所发展或是发生改变。
    刚开始运行效率还挺快,但是当文本量起来后就慢了,所以如何提高下面命令的效率
    ```
    *>&1 | ForEach-Object {if($_ -notin (Get-Content 888.txt)){Add-Content 888.txt $_ -Encoding utf8; $_}}

    ```

    规则是“整行”内容匹配,而不是包含”内容匹配

    解释一下,上面是将 888.txt 里的内容跟 powershell 控制台的输出进行比对,也就是说,控制台输出了“hello world”的话,就会比对 888.txt 有没有相同的内容,如果 888.txt 里也有一整行写着“hello world”则不追加写入 txt
    4 条回复    2023-02-18 05:27:56 +08:00
    fenglala
        1
    fenglala  
       2023-02-17 08:33:46 +08:00 via Android
    mudssky
        2
    mudssky  
       2023-02-17 09:01:05 +08:00
    避免多次读取和写入同一个文件。可以将已存在的文本内容加载到变量中,并将新内容添加到变量中,然后一次性将变量写入文件。这样可以减少磁盘 I/O 操作,提高执行速度。例如:

    $cOntent= Get-Content 888.txt -Raw
    Get-ChildItem -Path "C:\Path\To\Directory" -Recurse -ErrorAction SilentlyContinue | ForEach-Object {
    if ($_.Attributes -ne "Directory") {
    $newCOntent= Get-Content $_.FullName -Raw
    if ($newContent -notin $content) {
    $content += $newContent
    }
    }
    }

    Set-Content 888.txt -Value $content -Encoding utf8


    帮你查的
    darklights
        3
    darklights  
       2023-02-17 10:01:41 +08:00
    空间换时间

    $dict = @{}; gc a.txt | ?{$_ -notmatch '^\s*$'} | %{$dict[$_]=1}
    gc b.txt | ?{$_ -notmatch '^\s*$'} | ?{-not $dict[$_]} | Add-Content a.txt
    1054850490
        4
    1054850490  
    OP
       2023-02-18 05:27:56 +08:00
    @fenglala 但是这个并不是 powershell ,而是 bash
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     958 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 19:40 PVG 03:40 LAX 12:40 JFK 15:40
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86