AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1841182
Accepted
Clamarc
Clamarc
Asked: 2024-05-03 06:44:11 +0800 CST2024-05-03 06:44:11 +0800 CST 2024-05-03 06:44:11 +0800 CST

Powershell-仅保留txt文件中包含某些选定单词的行

  • 772

我有一个 txt 文件,我想只保留其中的几行

我想要一个命令来改变这个

txt 文件:

;FFMETADATA1
title=TNT
disc=01
comment=
lyrics-eng=
\

\

\
album=Classic
genre=Older
artist=ACDC
encoder=Lavf59.17.102

进入这个:

;FFMETADATA1
title=TNT
encoder=Lavf59.17.102

我使用了这个链接中的命令:

仅保留 txt 文件中包含特定单词/字符的行 (Powershell)(Windows 批处理文件)

PowerShell -Command "gci *.txt | foreach { (cat $_.FullName | ? { $_ -like '*;FFMETADATA1*' }) | set-content $_.FullName}"

但txt文件是这样的:

;FFMETADATA1

如何使用命令使txt文件有行;FFMETADATA1 title=和encoder=?

windows
  • 3 3 个回答
  • 152 Views

3 个回答

  • Voted
  1. Best Answer
    SimonS
    2024-05-04T07:54:23+08:002024-05-04T07:54:23+08:00

    你的命令基本上是正确的,或者说你走在正确的道路上。但-like你应该使用-matchand 正则表达式。'METADATA|title|encoder'将返回 txt 中与 3 个字符串中的任何一个匹配的任何行。所以命令是:

    如果你想创建一个新文件:

    cat .\su.txt | ? { $_ -match 'METADATA|title|encoder' } | set-content .\su2.txt
    

    如果要将其保存在同一个文件中:

    (cat .\su.txt | ? { $_ -match 'METADATA|title|encoder' }) | set-content .\su.txt
    

    对于目录中的所有文件:

    gci *.txt | foreach { (cat $_.fullname | ? { $_ -match 'METADATA|title|encoder' }) | set-content $_.fullname  }
    

    如果我们要进行代码高尔夫,我们甚至可以通过删除? (Where-Object)( 并使用scfor set-content- 你实际上不应该使用太多别名来缩短它,因为它会使内容更难阅读,但我现在已经在代码高尔夫了,我不能停止)

    (cat .\su.txt) -match 'METADATA|title|encoder' | sc bla.txt
    

    更具体地说,您可以随时调整正则表达式。例如:';FFMETADATA1|title=TNT|encoder=Lavf59.17.102'

    如果您的正则表达式变得难以阅读,请让 PowerShell 为您创建它

    $Regex = @(
        ";FFMETADATA1",
        "title=TNT",
        "encoder=Lavf59.17.102"
    ) -join '|'
    
    (cat .\su.txt) -match $Regex | set-content bla.txt
    

    编辑:根据 OP 的请求 - 如何通过 cmd 和隐藏的 PS 窗口运行它:

    PowerShell -WindowStyle Hidden -Command "gci *.txt | foreach { (cat $_.fullname | ? { $_ -match 'METADATA|title|encoder' }) | set-content $_.fullname  }"
    
    • 3
  2. LPChip
    2024-05-03T19:55:50+08:002024-05-03T19:55:50+08:00

    您可以使用以下脚本来获得您想要的内容。要么将其保存到 .ps1 文件并执行,要么直接将其粘贴到 powershell 中并按 Enter。

    不要忘记编辑路径。

    $files = Get-ChildItem "C:\Temp\test" -Filter "*.txt"
    
    $files | ForEach-Object {
        $content = Get-Content -path $_.FullName -Force
        
        $newfile = @()
    
        $matches = @(
            ";FFMETADATA1"
            "title="
            "encoder="
        )
    
        $content | ForEach-Object {
            $line = $_
    
            $matches | ForEach-Object {
                if( $line.StartsWith($_) ) 
                {
                    $newfile += $line
                }
            }
        }
    
        rename-item -Path $_.FullName -NewName "$($_.FullName).old"
        
        $newfile | Out-File $_.FullName
    }
    
    • 1
  3. Mr.Key7
    2024-05-03T21:16:50+08:002024-05-03T21:16:50+08:00

    为此,使用 PowerShell 比 Batch 更容易。

    - 快捷方式

    $Text    = ".\text.txt"
    $toKeeps = ';FFMETADATA1', 'title=TNT', 'encoder=Lavf59.17.102'
    $Null > $Text
    $toKeeps | % {$_} >> $Text
    

    - 没有备份

    $Text    = ".\text.txt"
    $toKeeps = ';FFMETADATA1', 'title=TNT', 'encoder=Lavf59.17.102'
    $NewText = cat $Text | ? {$_ -in $toKeeps}
    $Null > $Text
    $NewText | % {$_} >> $Text
    

    - 带备份

    $Text   = ".\text.txt"
    $Backup = "$($Text).bak"
    if ( !(Test-path $Text) )   {'File not found'; pause; exit 0}
    if ( !(Test-path $Backup) ) {Copy-Item $Text $Backup}
    $toKeeps = @(";FFMETADATA1"; "title=TNT"; "encoder=Lavf59.17.102")
    $NewText = Get-Content $Text | Where {$_ -in $toKeeps}
    $Null > $Text
    $NewText | ForEach {$_} >> $Text
    

    他们的输出:

    ;FFMETADATA1
    title=TNT
    encoder=Lavf59.17.102
    
    • 0

相关问题

  • 如何在 Windows Precision 触摸板上禁用鼠标加速?

  • 批量重命名图像文件集

  • Python 的“pass”参数的批处理等价物是什么?

  • 在 Windows 上与 Docker 守护进程通信

  • 资源管理器侧面板中的桌面外壳快捷方式

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve