汎用機メモっとくか

しごと用の(学習メモ&お気に入り保存)。

でっきるかな?PowerShell 007

2023年07月09日 17時25分25秒 | PowerShell

 

1..10|&{begin{$a="abc";$c=0}process{$c=$c+$_;echo ($a+$_)}end{echo (" sum=" + $c)}}


でっきるかな?PowerShell 005 PowerShellでfoldコマンド

2018年12月07日 01時47分56秒 | PowerShell

環境 Windows10 64bit PowerShell version 5.0 にて動かす機会があった。
性能のいいマシンならそこそこ動くと思われる。

 Windows7 32bit PowerShell version 2.0 の低スペックノートPCでは、

メモリを大量消費の上、速度もコード量の割に遅い

[foldで150バイト]

[byte[]]$crlf = @(13,10)
gc fixed150.txt -encoding byte -readcount 150  -totalcount (150*200) |%{$_ + $crlf} | sc out_Fixed150.txt -encoding byte


[1レコードの先頭35バイトを取り出す]
[byte[]]$crlf = @(13,10)
gc fixed150.txt -encoding byte -readcount 150  -totalcount (150*200) |%{$_[0..34] + $crlf} | sc out_Fixed150.txt -encoding byte


参考元

プログラマブルPowerShell ~プログラマのための活用バイブル
~ (.NET TECHNOLOGYシリーズ)
単行本(ソフトカバー) ? 2008/1/8
荒井 省三 (著)

単行本(ソフトカバー): 400ページ
出版社: 技術評論社 (2008/1/8)
言語: 日本語
ISBN-10: 4774133329
ISBN-13: 978-4774133324
発売日: 2008/1/8

3-2-5 バイナリファイルを処理する P240
より

 

<20181208追記STA>PowerShell SJIS固定長 改行(CrLf)なし 分割サンプル
※ハイスペックマシンでないと帰ってこないかも

 

[int[]]$arr = @(2,15,10,8,115);[byte[]]$crlf = @(13,10);[byte[]]$tab = @(9);$MAX=($arr.length -1)
gc fixed150.txt -encoding byte -readcount 150 -totalcount (150*200) |%{
$buf = $_
$j = 0
for($i=0;$i -lt $arr.length;$i++){
    $jx = $j + ($arr[$i] - 1)
    Write-Output $buf[$j..$jx]
    if($i -ne $MAX){Write-Output $tab}
    $j = $j + $arr[$i]
}
Write-Output $crlf} | sc out_Fixed150bbb.txt -encoding byte

 

<2行にしたもの>
[int[]]$arr = @(2,15,10,8,115);[byte[]]$crlf = @(13,10);[byte[]]$tab = @(9);$MAX=($arr.length -1)
measure-command{gc fixed150.txt -encoding byte -readcount 150 -totalcount (150*200) |%{$buf=$_;$j=0;for($i=0;$i -lt $arr.length;$i++){$jx=$j+($arr[$i]-1);Write-Output $buf[$j..$jx];if($i -ne $MAX){Write-Output $tab};$j=$j+$arr[$i];}Write-Output $crlf} | sc out_Fixed150.txt -encoding byte}

 

 <結果>
PS C:\Users\user\desktop> measure-command{gc fixed150.txt -encoding byte -readcount 150 -totalcount (150*200) |%{$buf=$
_;$j=0;for($i=0;$i -lt $arr.length;$i++){$jx=$j+($arr[$i]-1);Write-Output $buf[$j..$jx];if($i -ne $MAX){Write-Output $t
ab};$j=$j+$arr[$i];}Write-Output $crlf} | sc out_Fixed150.txt -encoding byte}

Days : 0

Hours : 0
Minutes : 0
Seconds : 5
Milliseconds : 825
Ticks : 58255296
TotalDays : 6.74251111111111E-05
TotalHours : 0.00161820266666667
TotalMinutes : 0.09709216
TotalSeconds : 5.8255296
TotalMilliseconds : 5825.5296

PS C:\Users\user\desktop>

 

<20181208追記END>


<20181209追記STA>

文字列を特定の長さで改行する方法
https://social.technet.microsoft.com/Forums/ja-JP/d962931a-55cc-4bc5-9661-d554c78137ae/25991233832101512434293052345012398382631237312391259133489212?forum=powershellja

<改変>version2.0ではSet-Content -encoding byteが遅いので速度いまいち。ファイルに書き出します。画面ではみれません。
[byte[]]$crlf=@(13,10)
$WindowSize = 150
$File = [System.IO.File]::OpenRead("C:\Users\user\Desktop\FIXED150.txt")
$Stream = New-Object System.IO.BinaryReader $File
0 .. (($File.Length - 1) / $WindowSize) |% { $Stream.ReadBytes($WindowSize) + $CrLf} | SC C:\Users\user\Desktop\OUT_FIXED150.txt -encoding byte;$File.close()


<いっそ.NETプログラミング版>ファイルに書き出します。画面ではみれません。
$len = 150
[byte[]]$crlf=@(13,10)
[byte[]]$buf     #(=>これは不要 要素数指定いらない) =@(0..($len-1))
$oFin = New-Object System.IO.Filestream ("C:\Users\user\Desktop\FIXED150.txt",[System.IO.FileMode]::Open)
$oBRin = New-Object System.IO.BinaryReader $oFin

$oFout = New-Object System.IO.Filestream ("C:\Users\user\Desktop\outFIXED150.txt",([System.IO.FileMode]::Create),([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout
while ($True){
    $buf = $oBRin.ReadBytes($len)
    if($buf.length -eq 0){
        break
    }else{
        $oBRout.Write($buf)
    }
    $oBRout.Write($crlf)
}

$oFin.Close();$oFout.Close();


 

<改変2>
[byte[]]$crlf=@(13,10)
$WindowSize = 150
$File = [System.IO.File]::OpenRead("C:\Users\user\Desktop\FIXED150.txt")
$Stream = New-Object System.IO.BinaryReader $File
$oFout = New-Object System.IO.Filestream ("C:\Users\user\Desktop\outFIXED150_999.txt",([System.IO.FileMode]::Create),([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout
0 .. (($File.Length - 1) / $WindowSize) |% { $oBRout.Write($Stream.ReadBytes($WindowSize)); $oBRout.Write($CrLf)} ;$File.close();$oFout.close();

<4件で試した結果>

PS C:\Users\user> $buf = $oBRin.ReadBytes($len)
PS C:\Users\user> $buf.length
150
PS C:\Users\user> $buf = $oBRin.ReadBytes($len)
PS C:\Users\user> $buf.length
150
PS C:\Users\user> $buf = $oBRin.ReadBytes($len)
PS C:\Users\user>
PS C:\Users\user> $buf.length
150
PS C:\Users\user> $buf = $oBRin.ReadBytes($len)
PS C:\Users\user> $buf.length
150
PS C:\Users\user> $buf = $oBRin.ReadBytes($len)
PS C:\Users\user> $buf.length
0

<20181209追記END>

 

<20181211追記STA><分割試行>[int[]]$arr = @(2,15,10,8,115)で

measure-command{
$len = 150
[byte[]]$crlf=@(13,10);[byte[]]$tab=@(9);
[byte[]]$buf #(=>これは不要 要素数指定いらない)=@(0..($len-1))
$oFin = New-Object System.IO.Filestream ("C:\Users\user\Desktop\Fixed150.txt",[System.IO.FileMode]::Open)
$oBRin = New-Object System.IO.BinaryReader $oFin

$oFout = New-Object System.IO.Filestream ("C:\Users\user\Desktop\oout.txt",([System.IO.FileMode]::Create), ([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout

while ($True){
    $buf = $oBRin.ReadBytes(2)
    if($buf.length -eq 0){
        break
    }else{
        $oBRout.Write($buf) ; $oBRout.Write($tab)
        $oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
        $oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
        $oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
        $oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($crlf)
    }
}
$oFin.Close();$oFout.Close();
}

 

Days : 0
Hours : 0
Minutes : 0
Seconds : 28
Milliseconds : 179

 <20181211追記END>

 <20181211追記2STA>

measure-command{
$len = 1500
[byte[]]$crlf=@(13,10);[byte[]]$tab=@(9);
[byte[]]$buf #(=>これは不要 要素数指定いらない)=@(0..($len-1))
$oFin = New-Object System.IO.Filestream ("C:\Users\user\Desktop\Fixed150.txt",[System.IO.FileMode]::Open)
$oBRin = New-Object System.IO.BinaryReader $oFin

$oFout = New-Object System.IO.Filestream ("C:\Users\user\Desktop\ooooooout.txt",([System.IO.FileMode]::Create),([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout

while ($True){
$buf = $oBRin.ReadBytes(2)
if($buf.length -eq 0){
break
}else{
$oBRout.Write($buf) ; $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($crlf)
}
}
$oFin.Close();$oFout.Close();
}

 <20181211追記2END>


 

<20181219追記STA>簡易版(少し遅くなります)
measure-command{
$len = 150
[int[]]$width = @(2,15,10,8,115)
$MAX  = ($width.length - 1)
[byte[]]$crlf=@(13,10);[byte[]]$tab=@(9);
[byte[]]$buf #(=>これは不要 要素数指定いらない)=@(0..($len-1))
$oFin   = New-Object System.IO.Filestream ("C:\Users\tkhs1732\Desktop\Fixed150.txt",[System.IO.FileMode]::Open)
$oBRin  = New-Object System.IO.BinaryReader $oFin

 

$oFout  = New-Object System.IO.Filestream ("C:\Users\tkhs1732\Desktop\oout2.txt",([System.IO.FileMode]::Create), ([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout

 

while ($True){
    $buf = $oBRin.ReadBytes($width[0])
    if($buf.length -eq 0){
        break
    }else{
        $oBRout.Write($buf) ; $oBRout.Write($tab)

 

        for($i=1;$i -lt $MAX;$i++){
            $oBRout.Write($oBRin.ReadBytes($width[$i])); $oBRout.Write($tab)
        }
        $oBRout.Write($oBRin.ReadBytes($width[$MAX]))  ; $oBRout.Write($crlf)
    }
}
$oFin.Close();$oFout.Close();
}

 <20181219追記END>

 

<20181220追記STA>簡易版(少し遅くなります)CrLfありの固定長の分割
measure-command{
$len = 150 + 2   #"+ 2"はCrLf分
[int[]]$width = @(2,15,10,8,115)
$MAX  = ($width.length - 1)
[byte[]]$crlf=@(13,10);[byte[]]$tab=@(9);
[byte[]]$buf #(=>これは不要 要素数指定いらない)=@(0..($len-1))
$oFin   = New-Object System.IO.Filestream ("C:\Users\tkhs1732\Desktop\crlfFixed150.txt",[System.IO.FileMode]::Open)
$oBRin  = New-Object System.IO.BinaryReader $oFin

$oFout  = New-Object System.IO.Filestream ("C:\Users\tkhs1732\Desktop\crlf_oout2.txt",([System.IO.FileMode]::Create), ([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout

while ($True){
    $buf = $oBRin.ReadBytes($width[0])
    if($buf.length -eq 0){
        break
    }else{
        $oBRout.Write($buf) ; $oBRout.Write($tab)

        for($i=1;$i -lt $MAX;$i++){
            $oBRout.Write($oBRin.ReadBytes($width[$i])); $oBRout.Write($tab)
        }
        $oBRout.Write($oBRin.ReadBytes($width[$MAX]))  ; $oBRout.Write($crlf)
        $DUMMY = $oBRin.ReadBytes(2) #末尾のCrLf分を読み取るだけの処理
    }
}
$oFin.Close();$oFout.Close();
}


<20181220追記END>

 



 


でっきるかな?PowerShell 004

2018年11月24日 01時27分29秒 | PowerShell

たっぷす庵 様 「PowerShellでifの戻り値パイプ出来ないの巻」
空のパイプ要素は許可されていません。
for文でパイプ
即時関数

「Cookie の使用に同意するものと見なされます。」がでます。

https://stuncloud.wordpress.com/2016/01/13/powershell_cant_pipe_if/

 

環境 Windows7 32bit PowerShell version 2.0 

PS C:\Users\user\desktop> for($i=0;$i -lt 16;$i++){"ITドカタの世界へようこそ $i"}|out-file -encoding default a777.txt
空のパイプ要素は許可されていません。
発生場所 行:1 文字:46
+ for($i=0;$i -lt 16;$i++){"ITドカタの世界へようこそ $i"}| <<<
    + CategoryInfo          : ParserError: (:) []、ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement

PS C:\Users\user\desktop>


PS C:\Users\user\desktop> &{for($i=0;$i -lt 16;$i++){"ITドカタの世界へようこそ $i"}}|out-file -encoding default a777.txt

PS C:\Users\user\desktop>

 

<20181127追記STA>PowerShellでSJIS固定長CrLf(改行)なしにCrLf(改行コード)を付加してレコード単位にする


インフラSEの運用・構築メモ 様
[PowerShell] テキストファイルをバイト単位で切り出す
http://rtaki.sakura.ne.jp/infra/?p=2783

 

(1)$enc = [System.Text.Encoding]::Default
(2)$bytes = $enc.GetBytes(($f_text = gc FIXED150.txt))
 (2-1)$f_text.length でレングス確認(文字数になる)
もしくは
   $bytes = $enc.GetBytes((gc FIXED150.txt))
 (2-2)$bytes.length  で確認(バイト数になる)
(3)$len = 150  #<=忘れると無限ループにはまります。要注意。
(4)&{for($i=0;  $i -lt $bytes.length; $i = $i + $len){$enc.GetString($bytes,$i,$len)}} | out-file out_fixed150.txt -encoding default

 <20181127追記END>

 

<20181129追記STA>PowerShellでCrLf(改行コード)なしSJIS固定長を項目単位に分割する。タブ区切り化。
PS C:\Users\user> cd desktop
PS C:\Users\user\desktop> $enc = [System.Text.Encoding]::Default
PS C:\Users\user\desktop> $bytes = $enc.GetBytes((gc FIXED150.txt))
PS C:\Users\user\desktop> $bytes.length
150000
PS C:\Users\user\desktop> $len = 150 #<=忘れると無限ループにはまります。要注意。
PS C:\Users\user\desktop> [int[]]$arr = @(2,15,10,8,115)
PS C:\Users\user\desktop> [string[]]$item = @(0..4)
PS C:\Users\user\desktop>

 

&{for($i=0; $i -lt $bytes.length; $i= $i + $len){
    $pos = $i
    for($j=0; $j -lt $arr.length; $j++){
        $item[$j] = $enc.GetString($bytes, $pos, $arr[$j])
        $pos = $pos + $arr[$j]
    }
    $item -join "`t"
 }
} | out-file a888.txt -encoding default

 

<20181129追記END>

<固定長の種>

'<Fixed_seed.vbs>
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim TargetFile 'As String
    Dim oFS 'As Object
    Dim oFR 'As Object
    Dim oFW 'As Object

    Dim i 'As Long
    Dim out_area(4)
    Dim KBN

    Call Make_out
    TargetFile = "FIXED150.txt"

    Set oFS = CreateObject("Scripting.FileSystemObject")

    Set oFW = oFS.OpenTextFile(TargetFile, ForWriting, True)

    For i = 1 To 1000
        'KBN = Int(Rnd()*5)
        KBN = "02"
        'CrLf付き
        'oFw.Writeline out_Area(KBN)
        'Crlf無し
        oFw.Write Right("00" & i,2)  & MID(out_Area(KBN),3,147)
    Next

    oFW.Close

    Set oFW = Nothing
    Set oFS = Nothing
    MsgBox "処理終了"

Function Make_out()
    out_area(0) = "00" & _
                  String(005,"A") & "#" & _
                  String(141,"B") & "#"
    out_area(1) = "01" & _
                  String(014,"C") & "#" & _
                  String(059,"D") & "#" & _
                  String(072,"E") & "#"
    out_area(2) = "02" & _
                  "FあFFいいFFFう#" & _
                  "GかGGきGG#" & _
                  "HHHさHH#" & _
                  String(114,"J") & "#"
                  'out_area(2) = "02" & _
                  ' String(014,"F") & "#" &
                  ' String(009,"G") & "#" &
                  ' String(007,"H") & "#" &
                  ' String(114,"J") & "#"
    out_area(3) = "03" & _
                  String(014,"K") & "#" & _
                  String(059,"L") & "#" & _
                  String(023,"M") & "#" & _
                  String(001,"N") & _
                  String(047,"P") & "#"
    out_area(4) = "99" & _
                  String(008,"Q") & "#" & _
                  String(138,"R") & "#"
End Function

<20181130追記STA>

文字列を特定の長さで改行する方法

「Linuxのfoldコマンドと同等のことを、PowerShellで行いたい」

https://social.technet.microsoft.com/Forums/ja-JP/d962931a-55cc-4bc5-9661-d554c78137ae/25991233832101512434293052345012398382631237312391259133489212?forum=powershellja

関係ないけど、パック10進数含むようなバイナリデータを扱うなら、perlが一番入手しやすくて、短く書けて、速くていいとおもいます。

perl58.dll <=versionでかわります。
perl.exe

さえあれば動きます。

「くんすとの備忘録」様
http://kunst1080.hatenablog.com/entry/2014/04/10/233257

固定長を区切るのも、以下は無理やりironRubyで書きましたけど、perlでできるし。

パック10進数を文字形式に変換するのも、そんなにむずかしくはないはず。

<自己記事>IronRubyでSJIS改行(CrLf)なし固定長めった斬り

https://blog.goo.ne.jp/tkhs1732/e/9edb7bb22c879f2076abc4e24e5b47a2

あとは、VB.NET か C#.NET。 さくっとその目的専用のコードを書いて(汎用なfoldをつくるのではない。作っていただけるとありがたいです。)、コンパイルして、exe作るとか?

<20181130追記END>


 

<20181201追記STA>全角"<" ">" を 半角"<" ">"にしてください。

PowerShellではありません。Perlですが、項目数が少なく かつ データ件数も10000件くらいなら


jperl -bpe "s/(.{2})(.{15})(.{10})(.{8})(.{115})/$1\t$2\t$3\t$4\t$5\n/g;"  FIXED150.TXT smosmo.txt


perl -pe "s/(.{2})(.{15})(.{10})(.{8})(.{115})/$1\t$2\t$3\t$4\t$5\n/g;"  FIXED150.TXT smosmo2.txt

$1~$10まで


こんなのでもいけます。

鬼車SED

onigsed -R --ctype=ASCII "s/(.{2})(.{15})(.{10})(.{8})(.{115})/\1\t\2\t\3\t\4\t\5\n/g;" FIXED150.TXT hiyohiyo.txt

下記どちらも\1~\9まで  

-r, --regexp-extended 
    use extended regular expressions in the script.
-R, --regexp-perl
     use Perl 5's regular expressions syntax in the script.

<20181201追記END>

<20181207追記STA>PowerShell
環境 Windows10 64bit PowerShell version 5.0 にて動かす機会があった。

性能のいいマシンならそこそこ動くと思われる。

 

Windows7 32bit PowerShell version 2.0 の低スペックノートPCでは、

 

メモリを大量消費の上、速度もコード量の割に遅い

[foldで150バイト風]

[byte[]]$crlf = @(13,10)
gc fixed150.txt -encoding byte -readcount 150  -totalcount (150*200) |%{$_ + $crlf} | sc out_Fixed150.txt -encoding byte


[1レコード先頭35バイトを取り出す]
[byte[]]$crlf = @(13,10)
gc fixed150.txt -encoding byte -readcount 150  -totalcount (150*200) |%{$_[0..34] + $crlf} | sc out_Fixed150.txt -encoding byte

<20181207追記END>




でっきるかな?PowerShell 003

2018年11月04日 08時38分31秒 | PowerShell

郵便番号データcsvをソートしてみる

環境 Windows7 32bit PowerShell version 2.0

$header ="a01","a02","a03","a04","a05","a06","a07","a08","a09","a10","a11","a12","a13","a14","a15"

PS C:\Users\user\desktop> gc 43KUMAMO.CSV |convertfrom-csv -header $header |sort a03 |convertto-csv |out-file -encoding default hoge2.txt

out-file -encoding default をいれないと Unicode(UTF-16 LE)になる

Sor-Objectで複数キー Asc Desc 指定できるみたいだけど、versionの為かうまくいかない

うまくいったら追記する

キーワード PowerShell ソート 複数キーでいっぱいひっかかるので参考にされたし

PS C:\Users\user\desktop> get-help Sort-Object -detailed

にでてる。長いので割愛。

 

 


でっきるかな?PowerShell 002

2017年05月11日 05時30分38秒 | PowerShell

でっきるかな?PowerShell 002

参考リンクは、後日まとめて記載。-STAいれるとマルチコアの恩恵は、受け取れなさそう。
ダイアログに力入れてないので、試すならShift_JISの".txt"を選んでください。

<実行>
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Users\user> PowerShell -STA -ExecutionPolicy RemoteSigned -f fileIO_01.ps1
PS C:\Users\user>

-STA は、dialog使うときだけ必要。

<fileIO_01.ps1>
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$dlgOP = New-Object System.Windows.Forms.OpenFileDialog
$dlgSV = New-Object System.Windows.Forms.SaveFileDialog

$RC_Open = $dlgOP.ShowDialog()

$dlgSV.FileName = "aaa001.txt"
$RC_Save = $dlgSV.ShowDialog()

if(($RC_Open -eq 1) -and ($RC_Save -eq 1)){
    $path_IN   = $dlgOP.FileName
    $path_Out  = $dlgSV.FileName
   
    $oSR = New-Object System.IO.StreamReader $path_IN  , ([System.Text.Encoding]::GetEncoding("Shift_JIS"))
    $oSW = New-Object System.IO.StreamWriter $path_OUT , False ,([System.Text.Encoding]::GetEncoding("UTF-8"))
   
    while($oSR.Peek() -ne -1){
        $oSW.WriteLine($oSR.ReadLine())
    }
   
    $oSR.Close()
    $oSW.Close()

<おまけ>Googleで"PowerShell ファイル分割"で検索してでてくる。
https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q13155160628

役にたちました。
改行コードLfでも読んでくれます(出力はCrLfになるけど)。
CPUシングルCOREだとめちゃめちゃ遅いです。
マルチCOREだと速い。

ShifJIS読むなら
-Encoding default;を

-Encoding String; 

 

参考文献&リンク

 うごかして学ぶWindows PowerShell
9-5.使い捨ての実行ポリシーをつかおう
Kindle版 柏原基規 (著)


.NETダイアログが表示されずスクリプトが止まる問題の対策
"読書とプログラミングを中心とした覚書ブログ"様
http://funcs.org/907

HOME http://funcs.org/

 

[PowerShell] ファイル分割スクリプト
”Effectiveさお”様
http://h-sao.com/blog/2014/07/17/divide-file-using-powershell/

=>さらに源流
powershellが超便利

ファイル分割スクリプト
”fivegangstersのブログ”様
http://blog.livedoor.jp/fivegangsters/tag/powershell

 

<20170523追記sta>PowerShellでレコード件数カウント

$c = 0;gc C:\Users\user\KEN_ALL.csv -ReadCount 30000 -Encoding String | % {  $c = $c + $_.Length };Write-Host $c"件"

PS C:\Users\user> $c = 0;gc C:\Users\user\KEN_ALL.csv -ReadCount 30000 -Encoding String | % { $c = $c + $_.Length };Wri
te-Host $c"件"
124114件
PS C:\Users\user>

 

<20170523追記sta>


<20170713追記sta>

 

「himorogiの日記」様 アーカイブ     (いろいろ後発だな私の記事、被ってるのあるし)
http://d.hatena.ne.jp/himorogi/archive

 


「コマンドの達人」様 PowerShell(バイナリ書き込み)
https://life-is-command.com/powershell-set-content/


<20170713追記end>