Powershell get specific lines
最近在Windows下处理数据,发现powershell还是挺好用的。
下面的是powershell的脚本,主要功能是提取指定行后指定的行数。
一般来说,使用powershell脚本时需要先获取权限。但我们也可以
使用未获取权限的脚本。可以使用下面的命令:
powershell.exe -ExecutionPolicy Bypass -File C:\mypowershell.ps1
或者
powershell -ep Bypass C:\MyUnsignedScript.ps1
详细的解答可以参考:
https://stackoverflow.com/documentation/powershell/5670/signing-scripts#t=201709050224117654371
<#
.SYNOPSIS
fileTruncation.ps1, Version 1.0
Truncate part of file and output into another file
.EXAMPLE
Extract 5 lines from the 4-th line of input.txt and store them into output.txt
powershell ./fileTruncation.ps1 input.txt output.txt 4 5
#>
[CmdletBinding(DefaultParametersetName='None')]
param(
[Parameter(Position=0,Mandatory=$true)] [string]$inputFileName,
[Parameter(Position=1,Mandatory=$true)] [string]$outputFileName,
[Parameter(Position=2,Mandatory=$true)] [long]$beginLine,
[Parameter(Position=3,Mandatory=$true)] [long]$totalLineCount
)
$ParamSetName = $PsCmdLet.ParameterSetName
function Show-Help {
param ( [string]$errormessage )
if ( $errormessage -ne "" ) {
Write-Host
Write-Host "Error: " -ForegroundColor Red -NoNewline
Write-Host $errormessage -ForegroundColor white
}
Write-Host
Write-Host "File truncation.ps1, Version 1.0"
Write-Host "Truncate part of file and output into another file"
Write-Host
Write-Host "Usage: " -NoNewline
Write-Host "powershell ./fileTruncation.ps1 inputfile outputfile beginLine totalTruncatedLines " -ForegroundColor green
Write-Host
Write-Host " Writed by Shouye Liu"
Write-Host
Exit 1
}
$sw = new-object System.Diagnostics.Stopwatch
$sw.Start()
# Get the full input and output paths
$fullInputPath = `
$ExecutionContext.SessionState.Path.`
GetUnresolvedProviderPathFromPSPath($inputFileName)
$fullOutputPath = `
$ExecutionContext.SessionState.Path.`
GetUnresolvedProviderPathFromPSPath($outputFileName)
# Make sure that the input file exists
if (-Not (Test-Path $fullInputPath))
{
Show-Help "File `"$fullInputPath`" not found"
}
if ( $h -or ( $fullInputPath -eq "" ) -or ( $fullInputPath -eq "/?" ) -or ( $fullInputPath -eq "--help" ) -or ( $fullOutputPath -eq "/?" ) -or ( $fullOutputPath -eq "--help" ) ) {
Show-Help
}
if ( $fullOutputPath -eq "" ) {
Show-Help "Please specify output files"
}
if ( [string]$invalidArgs -ne "" ) {
if ( ( $invalidArgs -eq "/?" ) -or ( $invalidArgs -eq "--help" ) ) {
Show-Help
} else {
Show-Help "Invalid command line argument(s)"
}
}
if ( $fullInputPath -eq $fullOutputPath ) {
Show-Help "The input file shouldn't be same as output file"
}
if ( ( $fullInputPath -eq $fullOutputPath ) -or ( $fullInputPath -eq "" ) -or ( $fullOutputPath -eq "" ) ) {
Show-Help
}
if ($beginLine -le 0 ){
Show-Help "beginLine should large than 0"
}
if ($totalLineCount -le 0){
Show-Help "totalLineCount should large than 0"
}
Write-Host "Input File Name: $inputFileName" -foregroundcolor "magenta"
Write-Host "Output File Name: $outputFileName" -foregroundcolor "magenta"
Write-Host "Beginning line Number: $beginLine " -foregroundcolor "magenta"
Write-Host "Total number of lines truncated: $totalLineCount lines" -foregroundcolor "magenta"
try
{
$reader = [System.IO.File]::OpenText($fullInputPath)
$writer = New-Object System.IO.StreamWriter($fullOutputPath)
for ($currentLine = 0;$currentLine -lt $beginLine-1;$currentLine++)
{
$line = $reader.ReadLine()
}
for (; $currentLine -le ($beginLine + $totalLineCount-2); $currentLine++)
{
$line = $reader.ReadLine()
if ($null -eq $line)
{
break;
}
$writer.WriteLine($line);
}
}
finally
{
Write-Host "Completed, wrote: $($currentLine - 1) totalLineCount to $outputFileName..."
if ($reader -ne $NULL)
{
$reader.Dispose();
}
if ($writer -ne $NULL)
{
$writer.Dispose();
}
$sw.Stop()
Write-Host " Consumes in " $sw.Elapsed.TotalSeconds "seconds"
}