powershell - How to extract lines between index 1 and index 2 to another csv variable? -
instead of outputting file set-content in how remove first , last line in powershell
$csv = import-csv in.csv -header date,time,o,h,l,c,v | select * -excludeproperty time | foreach {$_.date = [datetime]::parseexact($_.date,"yyyy.mm.dd",$null).tostring("yymmdd");$_.v=1;$_} | convertto-csv -notypeinformation ($i = 1; $i -lt ($csv.length - 1); $i++ { $csv[$i] -replace '"' | set-content out.csv -encoding ascii }
i want put these lines in $csv2 var instead of out.csv. set-content not work var. how (i don't have powershell 5 ) ?
the issue code every time call set-content rewrite file , replace content file have.
consider adding -append switch set-content. add file instead of overwriting it. remember make sure file empty befor begin writing it.
i consider using more simple way of getting "mid" content of file. check following sample. might not cover of requirements, simple way of getting in array except, first , last element using range operator.
# first setup test data $filecontent = @" line 1 skip please line 2 include line 3 include line 4 include line 5 include line 6 include line 7 include line 8 include line 9 include line 10 skip please "@ $filecontent | set-content in.csv $content = get-content in.csv $content[-($content.length-1)..-2] | set-content out.csv
Comments
Post a Comment