Inserting blank lines in powershell console
Asked Answered
M

4

19

On unix I can do a

tail -f file

And the equivalent powershell command is

gc file -Wait

However on unix I can press enter to add some blank lines (for readability) on the console while it is outputting lines, but not in powershell. Any workaround?


Use case: On unix I run tail -f /some/webserver/log/file and it outputs the last part of the log. I then do some http-requests and the log scrolls by accordingly. I then press enter a couple of times to get some blank space in the console, so the log entry for the next request stands out because of the blank lines above.

Mcgean answered 5/2, 2015 at 7:56 Comment(4)
Press Enter before the command?Andro
no I need the blank lines after the first output lines, i.e. so they stand out when I "do stuff that adds lines to the file"Mcgean
Can you copy and past what exactly you want the out put to look like .Varini
I added a description, hope that makes sense.Mcgean
V
29

You can use `n to create a new line .

This is just a small example , if you want to modife the out put of the Get-Content command you should store the out put and then and the new line to line 10 for example then retrieve the out put.

write-host "This is an example"
write-host "`n"
write-host "just to show how to add a new line"

This example reads a file and when it get to line to in inserts a space.

$content = Get-Content C:\Dump\test.txt
foreach ($line in $content)
{   
  if ($line.ReadCount -eq 2) { Write-Host "`n$line" }
  Else{$line}

}

This is the out put

Line 1

Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Varini answered 5/2, 2015 at 11:33 Comment(3)
No I want to do it interactively when I view a log file for instance, not print out a newline via some code :)Mcgean
The only way i could think to do this is have some thing read each line and thne wait for you to prsss 'Y' or 'N' and if you press 'Y' it creates a new line else if you press 'N' it just goes on to the next line with no spaceVarini
Hmm, I would probably have to multiplex keyboard input into i somehow, but having to manually scroll through a log by pressing 'N' would defeat the purpose (since i still would not be able to distinguish the relevant from irrelevant lines)Mcgean
E
9
Write-Host `n"hello"

the output is:


hello

This works because `n is a special character sequence, which represents a newline, in PowerShell.

Simply follow the link below, for additional information regarding the use of special characters in PowerShell:
Microsoft PowerShell Documentation - About Special Characters

Enchanter answered 21/3, 2020 at 1:19 Comment(1)
this is a fairly decent answer ... but explaining WHY it works would make it better. [grin]Osis
U
7

Best way to do blank lines for me is :

"" 

If you write that in script or console you get a blank line, just two double quotes.

If you want to create for example 3 new blank lines in one shot you could do :

"" ; "" ; "" 
Unexacting answered 30/11, 2017 at 14:4 Comment(3)
No I dont need to output newlines in the code, I need to interactively insert newlines in the powershell console.Mcgean
Ah i see you wrote : "add some blank lines (for readability) on the console while it is outputting lines". So you want to press enter to have blank lines WHILE the command is returning output? That's not possible unfortunatly. Powershell waits for the command to end then allows you to enter something else. Nothing can be input WHILE it's running.Unexacting
This could be a disaster if you use it within your PS functions. The captured outputs will have empty elements.Ablaut
T
2

Write-Host " "

I just copy this and keep it in clipboard to easily create empty spaces in my script presented to console.

I believe this is what he was looking for, This enables him to use the paste hotkey to easily accomplish his goal of keeping his output console run script clean by creating a fake space with that line.

Tectonics answered 26/5, 2022 at 3:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.