逐行读取文件并拆分值 [英] Read line-by-line file and split values
本文介绍了逐行读取文件并拆分值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要读取一个组成如下的txt文件:
I need to read a txt file composed as follow:
AA=1000,AA=320009#999999
AA=1011,AA=320303#111111
对于每条阅读的行,我需要用#"将其拆分以在第一回合获得
for each readed line I need to split it by "#" to get at the first turn
$test[0] = AA=1000,AA=320009 and $test[1]=999999
然后在第二个转弯处
$test[0] = AA=1000,AA=320003 $test[1]= 1111111
我在理解如何使用 get-content 或获取它时遇到了一些问题.
I have some problems to understand how to use get-content or to get it.
推荐答案
# Basically, Get-Content will return you an array of strings such as below:
# $tests = Get-Content "c:\myfile\path\mytests.txt"
$tests = @(
"AA=1000,AA=320009#999999",
"AA=1011,AA=320303#111111"
)
$tests | %{ $test = $_ -split '#'; Write-Host $test[0]; Write-Host $test[1] }
上一行相当于:
$tests | foreach {
$test = $_ -split '#'
Write-Host $test[0]
Write-Host $test[1]
}
这意味着对于 $tests 的每一行(每一行都被 $_ 实体化,一个对 '#' 进行拆分(这会产生一个用于 Write-Host 语句的数组)
Meaning that for each line of $tests (each line being materialized by $_, one does a split on '#' (which produces an array used in the Write-Host statements)
这篇关于逐行读取文件并拆分值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文