Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grab the first 10 characters of each line in a file, from Left to Right, in Powershell

Tags:

powershell

I'd like to take the first x amount of characters from $line and move them into a variable.

How do I do this?

Here is my existing code

$data = get-content "C:\TestFile.txt"
foreach($line in $data)
{
   if($line.length -gt 250){**get first x amount of characters into variable Y**}
}
like image 906
Noesis Avatar asked Sep 21 '12 16:09

Noesis


1 Answers

like this:

$data = get-content "TestFile.txt"
$amount = 10;
$y= @()
foreach($line in $data)
{
   if ( $line.length -gt 250){ $y += $line.substring(0,$amount) } 
}
like image 57
CB. Avatar answered Oct 07 '22 21:10

CB.