4

I am really 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 just started powershell two days ago so I don't know how to do this yet.

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
user350332
  • 173
  • 1
  • 2
  • 9

3 Answers3

8
  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
  • 344,408
  • 85
  • 689
  • 683
  • 1
    Just a note: redirection via `>` creates file in Unicode. If you need to specify encoding, use `...| Set-Content filename -encoding Utf8`. – stej Jun 23 '10 at 20:17
  • ... or `Out-File`, similarly. @stej: your “Unicode” meaning “UTF16-LE” in this case :-) – Joey Jun 24 '10 at 07:17
  • 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 – user350332 Jun 29 '10 at 17:42
  • @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 Jun 29 '10 at 19:05
  • 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 $b Below is my Complete Script. – user350332 Jul 09 '10 at 15:57
  • Count-object counts num ber of lines and "T {0:D9}" returns the Trailer like this: T 000000002 (2 is number of lines.) – user350332 Jul 09 '10 at 16:02
  • What about where line is not equal to `''` or whitespace? – CMCDragonkai Apr 05 '16 at 11:47
  • @CMCDragonkai: Then you adjust the `Where-Object` pipeline part accordingly. I tend to use `? { $_ -replace '\s' }` to filter everything that's either empty or just whitespace. Although I guess `? { [string]::IsNullOrWhitespace($_) }` is faster. – Joey Apr 05 '16 at 13:45
1

I also like the ? used in place of the where-object to trim it down just that much more.

Get-Content file.txt | ?{ $_ } > file2.txt
ATek
  • 815
  • 2
  • 8
  • 20
1
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
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
user350332
  • 173
  • 1
  • 2
  • 9