从 Windows 上的存档中提取并清理单个文件

问题描述 投票:0回答:1

以下 bash 代码从存档中解压缩单个文件,删除前 3 个字节,从文件的每一行中删除最后一个制表符分隔值 (

\t1
),然后保存它。如何将其重写为 .ps1 或 .bat 文件以在 Windows 上运行?

f='XRD.rasx'
unzip -p $f Data0/Profile0.txt | tail +4c | sed -e "s;\t1\r$;;g" > "${f}-0.tsv"

查看可用的 powershell 命令,看起来需要:

  1. 使用
    Copy-Item
  2. 复制文件,使其以 .zip 结尾
  3. 使用
    Expand-Archive
  4. 将整个存档(可能非常大)提取到某个临时文件夹
  5. 删除重命名的存档以及多余的解压文件和不需要的文件
  6. 删除
    'Data0/Profile0.txt'
    文件的前3个字节(我不知道怎么做)
  7. 使用
    \t1
     从每行删除 
    -replace
  8. 'Data0/Profile0.txt'
    复制回原始位置

这感觉需要的代码远远不止一行,并且需要从磁盘读取和写入不必要的文件。这感觉非常愚蠢且低效。一定有更好的处理方式吗,也许是用

cmd.exe

bash powershell cmd extract
1个回答
0
投票

您可以在

.ps1
脚本中使用以下命令:

$f = 'XRD.rasx'

# rename file to a .zip extension. Note the file should be in the same directoy as the .ps1 script.
Rename-Item -Path $f -NewName ($f + '.zip')

# extract the contents of the .zip file to the current directory
Expand-Archive -Path ($f + '.zip') -DestinationPath '.'

# Delete the .zip file.
Remove-Item -Path ($f + '.zip')

# remove the \t1 from each line of the 'Data0/Profile0.txt'
(Get-Content -Path 'Data0/Profile0.txt') -replace '\t1', '' | Set-Content -Path 'Data0/Profile0.txt'

# move the modified 'Data0/Profile0.txt' file to the desired location.
Move-Item -Path 'Data0/Profile0.txt' -Destination ($f + '-0.tsv')
© www.soinside.com 2019 - 2024. All rights reserved.