使用powershell读取文本文件中的一系列字符?

时间:2015-04-20 02:06:39

标签: powershell

我只是想让一个人选择他们的起点和终点并打印范围。有谁知道怎么样?

$file =  Get-Content C:\text.txt 
[int]$start = Read-Host "Where would you like to start in the file?"
[int]$end = Read-Host "How many characters do you want to see?"

Write-Host $file

2 个答案:

答案 0 :(得分:2)

正如马特指出的那样,我怀疑你是否曾尝试使用谷歌,因为the answer很容易找到...

但是......我也是一个懒惰的管理员......这是你答案的第一步......

 $a = gc u:\test.txt
 "$a".Substring(4, 6)

答案 1 :(得分:1)

[int]$start = Read-Host "How many characters would you like to skip into the file?"
[int]$end = Read-Host "How many characters would you like to view?"

# Migh be [System.Text.Encoding]::ASCII depending on how your text is encoded
$string = [System.Text.Encoding]::Unicode.GetString((Get-Content C:\getty.txt -encoding byte)[$start..($start + $end)])

Write-Host $string