Powershell检查每行的第一个字符的具体值 [英] Powershell check first character of each line for specific value

查看:478
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆