none
How can I use select-string to only output last phrase in file RRS feed

  • Question

  • So I'm trying to run a script that searches a particular file for the phrase "Default Printer:     PRINTERNAME". What I have so far is:

    cls

    $file = "c:\temp\print.log"

    Select-String -pattern "Default Printer:..." -path $File

    This is returning a lot more data than I'd like:

    c:\temp\print.log:864:9/9/2014 10:01:18 PM:  Default Printer:  HEIAISNF004
    c:\temp\print.log:890:9/10/2014 8:27:08 AM:  Default Printer:  HEIAISNF004
    c:\temp\print.log.log:944:9/11/2014 8:06:45 AM:  Default Printer:  HEIAISNF004
    c:\temp\print.log:1012:9/12/2014 7:51:15 AM:  Default Printer:  HEIAISNF004
    c:\temp\print.log:1108:9/16/2014 7:38:20 AM:  Default Printer:  HEIAISNF004
    c:\temp\print.log:1134:9/18/2014 8:42:46 AM:  Default Printer:  HEIAISNF004
    c:\temp\print.log:1244:9/24/2014 4:55:44 AM:  Default Printer:  HEIAISNF004

    I only want the last instance of the pattern (which happens to be the most recent), and I want to output the value (in this case HEIAISNF004) to a csv file.

    Any ideas on how I can do that?

    Sunday, September 28, 2014 10:54 AM

Answers

  • Sorry - I just saw that you included you select statement. 

    Try this:

    Select-String -pattern "Default Printer:..." -path $File | select -Last 1


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Sunday, September 28, 2014 1:26 PM
    • Marked as answer by Bob Brendemuehl Sunday, September 28, 2014 2:21 PM
    Sunday, September 28, 2014 1:25 PM

All replies

  • What have you tried so far? You say you have a script but have failed to post an example of what you have tried.

    ¯\_(ツ)_/¯

    Sunday, September 28, 2014 1:21 PM
  • Sorry - I just saw that you included you select statement. 

    Try this:

    Select-String -pattern "Default Printer:..." -path $File | select -Last 1


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Sunday, September 28, 2014 1:26 PM
    • Marked as answer by Bob Brendemuehl Sunday, September 28, 2014 2:21 PM
    Sunday, September 28, 2014 1:25 PM
  • You can use Select-Object to get the last one:

    Select-String -pattern "Default Printer:..." -path $File | 
      Select-Object  -Last 1

    Or you can use array index:

    @(Select-String -pattern "Default Printer:..." -path $File)[-1]


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Sunday, September 28, 2014 1:25 PM
    Moderator
  • using the Select -Last 1 worked! Now, how do I just get the actual value of "Default Printer"?

    ----------------
    cls

    $file = "c:\Temp\Print.log"

    Select-String -pattern "Default Printer:..." -path $File | select -Last 1
    ----------------

    Outputs

    C:\Temp\Print.log:1244:9/24/2014 4:55:44 AM:  Default Printer:  HEIAISNF004

    I'd like to set a variable $DefaultPrinter to the value "HEIAISNF004", but unsure how I can do that.


    Sunday, September 28, 2014 2:20 PM

  • if('C:\Temp\Print.log:1244:9/24/2014 4:55:44 AM:  Default Printer:  HEIAISNF004' -match 'Default Printer:\s+(?<p>.*)'){
         $matches['p']
    }else{
         Write-Host 'Not Found!' -fore red
    }


    ¯\_(ツ)_/¯

    Sunday, September 28, 2014 2:52 PM
  • except the log changes daily...
    Sunday, September 28, 2014 3:42 PM
  • except the log changes daily...
    You cannot vote on your own post                    
    if($linefound -match 'Default Printer:\s+(?<p>.*)'){
         $matches['p']
    }else{
         Write-Host 'Not Found!' -fore red
    }


    ¯\_(ツ)_/¯

    Sunday, September 28, 2014 4:11 PM