none
PowerShell - Get first x characters from each line of text RRS feed

  • Question

  • I've got a text document and want to preform two things: 1) delete first 5 lines and 2) for each line of text, grab only the first 8 characters.  To help me in my logic, I've got this broken down into steps, with each step creating a new file.  My problem is with the last step of the script, $file3 is never created and I'm clueless.

    cls
    $file1 = 'C:\cs1.txt'
    $file2 = 'C:\cs2.txt'
    $file3 = 'C:\cs3.txt'
    $lines = @()

    (Get-Content $file1 | Select-Object -Skip 5) | Set-Content $file2

    Get-Content $file2 | ForEach-Object {$line=$_[5..8]} | Add-Content $file3

    Tuesday, September 16, 2014 9:06 PM

Answers

  • You know how to fix that.  The code was not tested it was just to point the way.  You still have to learn how to use PowerShell and make it work for you.

    $f=0..4 |%{$f}


    ¯\_(ツ)_/¯

    • Marked as answer by bakerkr94 Wednesday, September 17, 2014 10:17 AM
    Wednesday, September 17, 2014 9:54 AM

All replies

  • I would use Substring

    $f=Get-Content $file1
    $f=0..4 |$f
    $f1=$f|%{$_.SubString(5,$_.Length-1)}
    $f1 | Out-File newfile.txt


    ¯\_(ツ)_/¯

    • Proposed as answer by Sam Boutros Tuesday, September 16, 2014 10:57 PM
    Tuesday, September 16, 2014 9:24 PM
  • getting the below error:

    Wednesday, September 17, 2014 9:27 AM
  • You know how to fix that.  The code was not tested it was just to point the way.  You still have to learn how to use PowerShell and make it work for you.

    $f=0..4 |%{$f}


    ¯\_(ツ)_/¯

    • Marked as answer by bakerkr94 Wednesday, September 17, 2014 10:17 AM
    Wednesday, September 17, 2014 9:54 AM
  • Yeah, still reading, watching and doing in the world of PowerShell... it's been exciting and I am learning tons, thanks for the help.
    Wednesday, September 17, 2014 10:18 AM
  • That was a mistake on my part but it showed that you were just new.  Look into the learning resources her.  The videos will get you over the hump.

    ¯\_(ツ)_/¯

    Wednesday, September 17, 2014 10:40 AM