truncate - Limit csv column length using PowerShell -
i'm trying adjust tab-delimited textfile without headers using powershell , have write output same tab-delimited textfile. source data follows;
- aaa \t bbbbbb \t ccccccccccc
- aaaa \t bbbb \t aaaabbbbbccccccc
now column number 3 should limited first 5 characters only, making output follows;
- aaa \t bbbbbb \t ccccc
- aaaa \t bbbb \t aaaab
how can accomplish this?
the following code import csv file using import-csv
, loop through lines, creating substring (5 in length) of 3rd column before writing line new csv file.
$oldcsv = "t:\old.csv" $newcsv = "t:\new.csv" import-csv -delimiter "`t" -path $oldcsv -header "1","2","3" | foreach-object { "{0}`t{1}`t{2}" -f $_.1,$_.2,($_.3).substring(0,[math]::min(5,($_.3).length)) >> $newcsv }
Comments
Post a Comment