如何使用powershell从txt文件中删除前几个字
我的一个host.txt文件显示如下的数据,我希望最后一个字在修剪后存在。如何使用powershell从txt文件中删除前几个字
我该怎么做?任何人都可以帮忙吗?
| caa3ab95-ecad-46fe-9905-ceac58853ffc | Test-1-my_instance-25ghikbbpip6 |
我想只从名称为 “测试-1-my_instance-25ghikbbpip6”
我试过下面methord但在所有
$hosts = Get-Content C:\host.txt
foreach ($line in $hosts)
{
$split1 = $line.trim("|")
$split2 = $split1.Split(",")[1]
echo $split2 >> C:\instance.txt
}
没有工作下列内容:
$hosts = Get-Content "C:\host.txt"
foreach ($line in $hosts)
{
$split1 = $line.trim("|")
$split2 = $split1.Split("|")[1]
Write-Output $split2 >> "C:\instance.txt"
}
将会输出:
Test-1-my_instance-25ghikbbpip6
例子:
PS C:\Windows\system32>
$hosts = Get-Content "C:\Users\me\Desktop\test.txt"
foreach ($line in $hosts)
{
$split1 = $line.trim("|")
$split2 = $split1.Split("|")[1]
Write-Output $split2 >> C:\instance.txt
}
PS C:\Windows\system32> Get-Content C:\instance.txt
Test-1-my_instance-25ghikbbpip6
为什么修剪?让零感觉,为什么保存到变量和写输出?直接写入文件 – 4c74356b41
为了与OP的编码风格保持一致,他可以理解他出错的地方。随时发布一个替代和更简洁的解决方案,供用户学习。 –
你不会再通过做同样的事情学习 – 4c74356b41
你为什么不分裂的'|'?你为什么试图分割','当字符串不包含逗号? – 4c74356b41
在你的foreach循环中使用$ line.Split('|')[2]。 –