Powershell get specific lines

最近在Windows下處理數(shù)據(jù),發(fā)現(xiàn)powershell還是挺好用的。

下面的是powershell的腳本,主要功能是提取指定行后指定的行數(shù)。

一般來說,使用powershell腳本時需要先獲取權(quán)限。但我們也可以

使用未獲取權(quán)限的腳本。可以使用下面的命令:


powershell.exe ?-ExecutionPolicy ? ? ? ?Bypass ? ? ? -File ? ? C:\mypowershell.ps1


或者

powershell ? ?-ep ? ? Bypass ? ? ?C:\MyUnsignedScript.ps1

詳細(xì)的解答可以參考:

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"

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,906評論 0 0
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,786評論 0 23
  • 陣雨。 這個年紀(jì)總是想著去玩去旅游,要買化妝品了,買很多漂亮的衣服啦。于是會花上一堆時間去搜索窮游,去看性價比最高...
    家住夕陽江上村閱讀 164評論 0 0
  • 早期的一組創(chuàng)意插圖,之前命名為壓力系列,現(xiàn)在看來似乎也有些不妥,其實應(yīng)該叫做束縛或者牽絆吧!你無法走向你想走的那條...
    waitine閱讀 1,105評論 12 21
  • 留守思念 文/靈璧曙光 風(fēng)載著思念追逐了很遠(yuǎn) 你前面的風(fēng)景延伸了你的視線 我盤點了每一輛折回的車子 沒有你的存在,...
    靈璧曙光閱讀 310評論 0 1