Force new lines in output

This topic contains 3 replies, has 3 voices, and was last updated by  postanote 2 weeks, 6 days ago.

  • Author
    Posts
  • #83248

    JD
    Participant

    Hello, I'm new to PS and wondering if anyone can help me. What I am trying to do is force the output of this script to create new lines for every string that it finds in our log file. Currently, the output from this script is one huge block of text. Any help would be greatly appreciated! Here is my full script:

    # This script will search a text file for a particular string.  
    # It is designed to return the number of strings found along with the actual line that the 
    string(s) were found in.
    # Arguments example: d:\powertest.txt,string
    
    
    
    
    $logfile_path = $args.get(0);
    $regex = $args.get(1);
    
    $Error.Clear();
    
    if ( !$logfile_path )
    {
      Write-Host "Message: Can't find ""logfile_path"" argument. Check documentation."
      exit 1
    }
    if ( !$regex )
    {
      Write-Host "Message: Can't find ""regex"" argument. Check documentation."
      exit 1
    }
    
    if ( Test-Path $logfile_path ) 
    { }
    else
    {
      Write-Host "Message: File $logfile_path not found."
      exit 1
    }
    
    $filename = split-path "$logfile_path" -leaf
    $t = (Get-Childitem env:temp).value
    $file_path = "$t\$usage-$filename-$position"
    
    if ( Test-Path $file_path )
    { 
      $known_rows = get-content $file_path 
    }
    else
     { 
      $known_rows = 0 
     }
    
    $resn = @()
    $resl = @()
    $total = (get-childitem $logfile_path | select-string -pattern $regex | select linenumber, line 
    | measure-object).count
    
    if ($Error.Count -ne 0) 
    {
      Write-Host "$($Error[0])"
      exit 1
    }
     if ( $total -lt $known_rows ) 
    {
      $known_rows = 0
      $known_rows > $file_path
    }
    
    $temp = $total - $known_rows
    $known_rows = $temp + $known_rows
    $known_rows > $file_path
    
     for ( $i = 0 ; $i -le $total; $i++ ) 
    {
      $resn += @($i)
      $resl += @($i) }
      $i = 1
      $stat = get-childitem $logfile_path | select-string -pattern $regex | select linenumber, line 
    | ForEach-Object {
      $resn[$i] = $_.linenumber
      $resl[$i] = $_.line
      $i = $i + 1
    }
    
    
    if ($temp -eq 0)
    {
      write-host "Statistic: $temp"
      write-host "Message: No newly found strings"
      exit 0
    }
     if ($temp -gt 0)
    {
      $lines = @()
      write-host "Statistic: $temp"
      If($temp -gt 1)
      {
            for ( $i = 1 ; $i -le $temp; $i++ ) 
            {
                  $lines += $resl[$count - $i]
            }
            write-host "Message: Number of newly found strings $temp. Lines that included the 
    error are. $lines"
      }
      else
      {
            $line = $resl[$resl.Count - 1]
            write-host "Message: Number of newly found strings $temp. Lines that included the 
    error are $line"
      }
    
    }
    

    Currently the output looks similar to this:
    Number of newly found strings 3. Lines that included the error are. 2017-10-26 17:18:30,004 INFO [com.company.workflow.ejb.ProcessRulesEJB]
    (EJB default – 7) processWorkflowRule()- rules to process! 2017-10-26 17:17:39,476 INFO [com.company.workflow.ejb.ProcessEnrollmentEJB] (EJB
    default – 2) processEnrollmentFile() – No enrollments were found 2017-10-26 17:17:29,991 INFO [com.company.workflow.ejb.ProcessRulesEJB] (EJB
    default – 4) processWorkflowRule()- rules to process!

    I want it to looks like this:
    Number of newly found strings 3. Lines that included the error are:
    2017-10-26 17:18:30,004 INFO [com.company.workflow.ejb.ProcessRulesEJB] (EJB default – 7) processWorkflowRule()- rules to process!
    2017-10-26 17:17:39,476 INFO [com.company.workflow.ejb.ProcessEnrollmentEJB] (EJB default – 2) processEnrollmentFile() – No enrollments were found
    2017-10-26 17:17:29,991 INFO [com.company.workflow.ejb.ProcessRulesEJB] (EJB default – 4) processWorkflowRule()- rules to process!

  • #83249

    iain Barnetson
    Participant

    I use `r`n

    • #83257

      JD
      Participant

      Where exactly would I use it?

  • #83266

    postanote
    Participant

    Firstly only use Write-Host if you are doing customer formatting or text coloring.
    Write-Host make your data useable on the pipeline because it clear it from the buffer.

    So, this...

    write-host "Statistic: $temp"
    write-host "Message: No newly found strings"

    and this...

    "Statistic: $temp"
    "Message: No newly found strings"

    ... will output to the screen.

    and btw, so would this...
    ($MyVariable = Get-Process)

    all with note Write-* needed.
    If you take away the parens, then it will not write to the console.

    Secondly, there are a whole list of these characters and their defined use.
    See the PowerShell Help files.

    help about_Special_Characters

    # Or this page
    'neolisk.com/techblog/powershell-specialcharactersandtokens '

    Lastly, as for you query, you can put them in line with you string or anywhere else you deem appropriate.

    "`n" means new line
    "`r" means carriage return
    "`t" means tab

    As for this short list..

    The below would give you two new lines, using this inline approach.

    "`nStatistic: $temp `nMessage: No newly found strings"

    ...one before the word Statistics and one before the sentence Message...

    So, the result of the above is...

    Statistic:
    Message: No newly found strings

    One could just as easily done this ...

    "`n"
    "Statistic: $temp"
    "`n"
    "Message: No newly found strings"

    ... and that results would be

    Statistic:

    Message: No newly found strings

    ..note the double spacing

    As you combine these formatting characters, of course you result will vary.

    So, If you did this...

    "`nStatistic: $temp `n`t`t`tMessage: No newly found strings"

    ... what would you think the result would be...

    ... No suspense, but the results would be

    Statistic:
    Message: No newly found strings

    ... two lines and 3 tabs.

    and so on.

    HTH

You must be logged in to reply to this topic.