以下 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 命令,看起来需要:
Copy-Item
Expand-Archive
'Data0/Profile0.txt'
文件的前3个字节(我不知道怎么做)\t1
从每行删除
-replace
'Data0/Profile0.txt'
复制回原始位置这感觉需要的代码远远不止一行,并且需要从磁盘读取和写入不必要的文件。这感觉非常愚蠢且低效。一定有更好的处理方式吗,也许是用
cmd.exe
?
您可以在
.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')