tags:

views:

63

answers:

2

Hi All, I am realy very new to powershell. I want to use powershell to read a txt file and change it to another format.

  1. Read from a txt file.
  2. Format Data( remove lines, remove blank spaces in between)
  3. Count of records ( "T 000000002" 9 chars)

and then write the output to a new file.

I have started powershell just 2 days ago so plz give me ideas abt how to go about this stuff.

+2  A: 
  1. Reading from a file:

    Get-Content file.txt
    
  2. Not quite sure what you want here. Get-Content returns an array of strings. You can then manipulate what you get and pass it on. The most helpful cmdlets here are probably Where-Object (for filtering) and ForEach-Object (for manipulating).

    For example, to remove all blank lines you can do

    Get-Content file.txt | Where-Object { $_ -ne '' } > file2.txt
    

    This can be shortened to

    Get-Content file.txt | Where-Object { $_ } > file2.txt
    

    since an empty string in a boolean context evaluates to false.

    Or to remove spaces in every line:

    Get-Content file.txt | ForEach-Object-Object { $_ -replace ' ' } > file2.txt
    
  3. Again, not quite sure what you're after here. Possible things I could think of from your overly elaborate description are something along the lines of

    $_.Substring(2).Length
    

    or

    $_ -match '(\d+)' | Out-Null
    $Matches[1].Length
    
Joey
Just a note: redirection via `>` creates file in Unicode. If you need to specify encoding, use `...| Set-Content filename -encoding Utf8`.
stej
... or `Out-File`, similarly. @stej: your “Unicode” meaning “UTF16-LE” in this case :-)
Joey
Hi all your help was very usefull. But I am still trying to figure out how to count the number of lines. i am able to remove spaces My code so far is$s = get-content D:\MyScripts\members.txt | Foreach-Object { ($_ -replace '-', '') } |Foreach-Object { ($_ -replace 'OP_ID', '') } | Foreach-Object { ($_ -replace 'EFF_DT', '') } |Where-Object { $_ -ne '' }|Remove-Spaces $s | set-content D:/MyScripts/nmembers.txt
@mahmgp: Store the result in a variable, which will be an `Object[]`. It has a `Length` property. You can then proceed saving it to the file. Or you use `Tee-Object` to send it to the file and append a `Measure-Object` at the end of the pipeline. The resulting object has a `Count` property.
Joey
Thanks Rossel, your suggestions were really help full. But Measure did not work for me. it returned the namespace in the output if i used Measure. Therefore i have used Count-Object $b "T {0:D9}" -f $bBelow is my Complete Script.
Count-object counts num ber of lines and "T {0:D9}" returns the Trailer like this: T 000000002 (2 is number of lines.)
A: 

function Count-Object() { begin { $count = 0 } process { $count += 1 } end { $count } }

$a= get-content .\members.txt |
Foreach-Object { ($_ -replace '\s','') } | Foreach-Object { ($_ -replace '-','') } | Foreach-Object { ($_ -replace 'OP_ID','') } | Foreach-Object { ($_ -replace 'EFF_DT','') } | Where-Object { $_ -ne '' }| set-content .\newmembers.txt

$b = Get-Content .\newmembers.txt | Count-Object $b "T {0:D9}" -f $b | add-content .\newmembers.txt