Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Need to display comment if line starts with hash

$
0
0

Hi powershell,

here is my sample powershell script as below:

$fileList = dir 'H:\Demo.txt'
$newExtension = ".CSV"

foreach ($file in $fileList){
$input = @(Get-Content $file |  ForEach-Object {($_ -replace "\s+",",")})
$input | ConvertFrom-Csv -Header (1..5)|
ForEach-Object { $_.'5' = 'aa'; $_ } |
ConvertTo-Csv -NoTypeInformation |
Select-Object -Skip 1 |
ForEach-Object { $_ -replace '"','' } |  Out-File -FilePath  "H:\$($file.BaseName)$newExtension" -Encoding OEM 
}

Where Demo.txt:

This is Comment 1
#This is Comment 2
This is Comment 3
AA BB CC
CC DD EE
FF GG HH

The output is:
This,is,Comment,1,aa
This,is,Comment,3,aa
AA,BB,CC,,aa
CC,DD,EE,,aa
FF,GG,HH,,aa

It looks fine but the problem is which i cannot see the second line. I want to display the second line comment if line starts with hash. How to fix it?

 

Thanks

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles