< Back

Set-Lines

Mon Jan 13, 2020 7:19 pm

NAME Set-Lines



SYNOPSIS

Set any text files to a fixed number of lines and optionally remove blank lines.





SYNTAX

Set-Lines -Filepath <String> [-Blanks] [-Hide] [-WhatIf] [-Confirm] [<CommonParameters>]





DESCRIPTION

The Set-Lines Cmdlet will maintain any text or log file to a fixed length and also optionally remove any blank

lines. Transcript files may also be formatted but only if the current session is not transcribing (achieved with

the Stop-Transcript command). Needs Administrator account.



In order to remove blank lines only, each individual file can be suffixed with '|0' or, if they are input via the

pipeline, the -BLANK switch will perform that option for each one. See the example section for exact details.



The progress bar will give a continuous display of the number of blank lines found and also whether they are being

removed.



*Important: any filepath containing the word 'transcript' will be formatted using Unicode rather than the default

type, so ensure that these originate from the PowerShell console. Conversely any PS console files not so named

will not be formatted correctly.





PARAMETERS

-Filepath <String>

The full path of the file to be processed followed by '|' and then 0 or the number of lines for the file

length.



Required? true

Position? 1

Default value None

Accept pipeline input? true

Accept wildcard characters? false



-Blanks

Remove all blank lines from any files in the input stream.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-Hide

(Alias quiet) Suppress the progress bar. This works in exactly the same way as the $progressPreference

variable.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-WhatIf

(Alias wi) Indicates what action will be performed without actually performing it. The cmdlet is not run.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Confirm

(Alias cf) Issues a prompt before continuing with the operation.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS









OUTPUTS









NOTES





The format of each Filepath input parameter is the full path together with the number of desired lines for

that file, ie "c:\\thisfile.txt|1500" will set the line limit to 1500 (Note the separator before the digits is

the same as the PowerShell 'Pipe').

Any deviation from this pattern will generate a probably confusing error message indicating the expected

input. This must be corrected before the cmdlet will execute. Either upper or lower case may be used.

In order to have any blank lines removed from any file use the suffix '|0', ie 'c:\\thisfile.txt|0'. Either

text or log files will be accepted. Files of less than 10 lines will not be processed.



If the pipeline input contains a mixture of the 2 previous examples, use the -BLANKS switch to remove blank

lines from all of the files.

In order to suppress the progress bar, use the -HIDE (alias -QUIET) switch, which is similar to setting

$ProgressPreference = SilentlyContinue. The progress bar will never appear for files of less than 100 lines.

The cmdlet can be used in background jobs where this feature can be useful as in this sample script below. All

ouput, including the Progress Bar (unless the -HIDE switch has been used), will still be displayed at the

console:



Stop-Transcript | Out-Null

$argumentList = 'd:\\scripts\\Transcript.txt|1500', '-BLANKS', '-HIDE'

$scriptBlock = {param([string]$Arg1, [string]$Arg2, [string]$Arg3)

Invoke-Expression -Command "Set-Lines $Arg1 $Arg2 $Arg3"}

Start-Job -Name Job1 -ScriptBlock $scriptBlock -argumentList $argumentList | Wait-job | Receive-Job

Start-Transcript -Append | Out-Null



-------------- Example 1: input a single file. --------------



C:\\PS>Set-Lines "d:\\scripts\\summary.log|1000"



This will limit the file to 1000 lines length and not remove blank lines. Use quotes if there are spaces or to

include something like "$pwd\\myfile.txt|2000".







PS D:\\Scripts> set-lines "d:\\scripts\\summary.log|1000"

16/01/2014 21:14:27 Starting maintenance on file <D:\\Scripts\\summary.log>

16/01/2014 21:14:32 Maintenance of file completed: 13 lines removed.





-------------- Example 2: using pipeline input. --------------



C:\\PS>"$pwd\\ipconfig.log|1500","$pwd\\transcript.txt|0" | Set-Lines



Remove all but the last 1500 lines from ipconfig.txt and all blank lines from transcript.txt. A warning will be

generated and the latter file skipped if Stop-Transcript has not been entered.







PS D:\\Scripts> "$pwd\\ipconfig.log|1500","$pwd\\transcript.txt|0" | set-lines

16/01/2014 21:14:27 Starting maintenance on file <D:\\Scripts\\ipconfig.log>

16/01/2014 21:14:32 Maintenance of file completed: 504 lines removed.

16/01/2014 21:14:33 Starting maintenance on file <D:\\Scripts\\transcript.txt>

16/01/2014 21:14:40 Maintenance of file completed: 81 blank lines removed.





-------------- Example 3: using the Blanks parameter. --------------



C:\\PS>Set-Lines "c:\\documents\\file.txt|500" -Blanks



Remove all but the last 500 lines of the file including any blank lines. Note that blanks will be removed from the

500 lines, leaving a lower number.







PS D:\\Scripts> set-lines "c:\\documents\\file.txt|500" -Blanks

16/01/2014 21:18:23 Starting maintenance on file <c:\\documents\\file.txt>

16/01/2014 21:18:30 Maintenance of file completed: 19 lines removed (+ 8 blank).







RELATED LINKS

http://www.SeaStar.99k.org