powershell - Text extraction and reformatting for output -
i have text file "e:\abc.txt" contains data in format
heading asia name china val 200 drt fun , play end heading europe name paris val 234234 drt tour end heading america name ny val 234 drt shop end [continued this..]
i create new text file "xyz.txt" extract selected headings , drt in following format.
heading drt asia fun , play europe tour
i have tried following script:
$source e:\abc.txt $search "asia","europe" $a = select-string $source -pattern $search -context (0,3)
output
heading asia name china val 200 drt fun , play heading europe name paris val 234234
how remove in between lines
heading - drt asia - fun , play europe - tour
whipped since have @ least been trying something. hope not go on head uses regular expressions break file "sections" converts each section own object. advantage being can filter using command powershell cmdlets where-object
. not terse said wanted whip easier follow.
$path = "e:\abc.txt" $headingstomatch = "asia","europe" (get-content $path | out-string) -split "end" | where-object{$_ -match "\w"} | foreach-object{ $hash = $_.trim() -split "`r`n" -replace "^\s+" -replace "^(.*?)\s",'$1=' | out-string | convertfrom-stringdata new-object -typename pscustomobject -property $hash } | where-object{$headingstomatch -contains $_.heading} | select-object heading,drt
using above text file this.
heading drt ------- --- asia fun , play europe tour
now can export file easy using export-csv
.
Comments
Post a Comment