#author("2017-07-05T13:31:51+09:00","","")
#navi(../)
* PowerShellでheadとtailを実現する [#vb3c79fd]
Linux(UNIX)にあるheadとtailコマンドをPowerShellで実現する方法を以下に記します。

#contents
#htmlinsertpcsp(win-top.html,win-sp.html)

* Select-Objectの-firstと-lastオプション [#l2b02650]
Select-Objectの-firstと-lastオプションを使用することにより、~
headとtailコマンドと同様の機能を実現することができます。

** -firstオプション [#l9376d57]
指定した数値分、先頭行を表示します。
 Select-Object -first 数値
以下に実際に動作させた例を記します。

以下の例では、Windowsフォルダのファイルリストから先頭の5件表示しています。
 PS C:\Windows> Get-ChildItem * | Select-Object -first 5


     Directory: C:\Windows


 Mode                LastWriteTime         Length Name
 ----                -------------         ------ ----
 d-----       2009/07/14     14:32                addins
 d-----       2016/04/04      9:14                AppCompat
 d-----       2017/07/03      9:42                AppPatch
 d-r-s-       2017/05/10     10:31                assembly
 d-----       2016/04/01     10:51                AsusInstAll

 PS C:\Windows> Get-ChildItem -Name * | Select-Object -first 5
 addins
 AppCompat
 AppPatch
 assembly
 AsusInstAll

以下の例では、最終更新日でソートしたファイルリストの先頭から5件表示します。
 PS C:\Windows> Get-ChildItem * | Sort-Object { $_.LastWriteTime } | Select-Object -first 5


     Directory: C:\Windows


 Mode                LastWriteTime         Length Name
 ----                -------------         ------ ----
 -a----       2009/06/11      5:30          53551 Professional.xml
 -a----       2009/06/11      5:31          48201 Starter.xml
 -a----       2009/06/11      5:36           1405 msdfmap.ini
 -a---l       2009/06/11      5:52         316640 WMSysPr9.prx
 ------       2009/06/11      6:08            219 system.ini

 PS C:\Windows> Get-ChildItem * | Sort-Object -Descending { $_.LastWriteTime } | Select-Object -first 5


     Directory: C:\Windows


 Mode                LastWriteTime         Length Name
 ----                -------------         ------ ----
 d-----       2017/07/05     12:26                Temp
 d-----       2017/07/05      9:17                System32
 d-----       2017/07/05      9:05                inf
 -a----       2017/07/05      9:01           9204 cfgall.ini
 -a----       2017/07/05      9:01          23260 TMFilter.log

以下の例では、ファイルでソートしたファイルリストの先頭から5件表示します。
 PS C:\Windows> Get-ChildItem * | Sort-Object { $_.Length } | Select-Object -first 5


     Directory: C:\Windows


 Mode                LastWriteTime         Length Name
 ----                -------------         ------ ----
 -a----       2016/04/01     10:51              0 Ascd_err.ini
 -a----       2016/04/01     10:51              0 scd.ini
 -a----       2017/05/22     16:56              0 setuperr.log
 d-----       2011/04/12     17:01                security
 d-----       2009/07/14     14:32                schemas

 PS C:\Windows> Get-ChildItem * | Sort-Object -Descending { $_.Length } | Select-Object -first 5


     Directory: C:\Windows


 Mode                LastWriteTime         Length Name
 ----                -------------         ------ ----
 -a----       2016/04/01     10:11        7831039 OFCNT.LOG
 -a---l       2016/01/22     14:19        3231232 explorer.exe
 --r---       2013/09/13     19:44        2080472 RtlExUpd.dll
              2017/07/04     14:59        1076207 WindowsUpdate.log
 -a---l       2017/06/02     17:10         733696 HelpPane.exe

** -lastオプション [#ca3a6f1a]
指定した数値分、最終行からさかのぼり表示します。
 Select-Object -last 数値
以下に実際に動作させた例を記します。
 PS D:\> Get-Content .\text.txt
 1 one
 2 two
 3 three
 4 four
 5 five
 6 six
 7 seven
 8 eight
 9 nine
 10 ten

 PS D:\> Get-Content .\text.txt | Select-Object -first 5
 1 one
 2 two
 3 three
 4 four
 5 five

 PS D:\> Get-Content .\text.txt | Select-Object -last 5
 6 six
 7 seven
 8 eight
 9 nine
 10 ten

以上、PowerShellでheadとtailと同様な動作を実現する、Select-Obectの-last, -firstオプションについての紹介でした。

#htmlinsertpcsp(win-btm.html,win-sp.html)


トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS