在PowerShell中检查每行的第一个字符以获取特定值 [英] Check first character of each line for a specific value in PowerShell
本文介绍了在PowerShell中检查每行的第一个字符以获取特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在阅读一个包含数字特定格式的文本文件。我想弄清楚该行的第一个字符是6还是4,并将整行存储在数组中以备后用。因此,如果该行以6开头,则将整个行添加到sixArray中,如果该行以4开头,则将整个行添加到fourArray中。
I am reading in a text file that contains a specific format of numbers. I want to figure out if the first character of the line is a 6 or a 4 and store the entire line in an array for use later. So if the line starts with a six add the entire line into sixArray and if the line starts with a 4 add the entire line into fourArray.
如何检查第一个字符,然后抓住该行上其余的X个字符?无需替换任何数据?
How can I check the first character and then grab the remaining X characters on that line? Without replacing any of the data?
推荐答案
像这样的事情可能会起作用。
Something like this would probably work.
$sixArray = @()
$fourArray = @()
$file = Get-Content .\ThisFile.txt
$file | foreach {
if ($_.StartsWith("6"))
{
$sixArray += $_
}
elseif($_.StartsWith("4"))
{
$fourArray += $_
}
}
这篇关于在PowerShell中检查每行的第一个字符以获取特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文