Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 140
  • Last Modified:

How do I get Powershell to write values to different lines?

Here is a script, however it does not write the values of Total and Sub to different lines.

I would like it to write values to different lines.

$InputFile = "C:\Data\original.txt"
$OutputFile = "C:\Data\NewData.txt"

[double]$Total = 0
($Input = Get-Content -Path $InputFile) | ? {$_ -match "^LO1"} | % {
      $Columns = $_.Split("*")
      $Total += ([double]$Columns[2]) * [double]($Columns[4])
}
$Input[0], ("Total: {0:C}" -f $Total)

[double]$Sub = 0
($Input = Get-Content -Path $InputFile) | ? {$_ -match "^GO1"} | % {
      $Columns = $_.Split("*")
      $Test += ([double]$Columns[2]) * [double]($Columns[4])
}
$Input[0], ("Subtotal: {0:C}" -f $Sub) | Set-Content ($Total,$Sub) -Path $OutputFile -Encoding UTF8


Get-Content -Path $OutputFile
0
100questions
Asked:
100questions
  • 7
  • 7
1 Solution
 
becraigCommented:
Simply change the last line

$Input[0], ("Subtotal: {0:C}" -f $Sub) | Set-Content ("$Total `n$Sub") -Path $OutputFile -Encoding UTF8

Open in new window

0
 
100questionsAuthor Commented:
Do you mean to replace Get-Content -Path $OutputFile with your command?
0
 
becraigCommented:
Change the set-content to have a new line
Set-Content ("$Total `n$Sub`n")

Open in new window


That way after writing $total it will create a new line and then write sub
0
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
100questionsAuthor Commented:
It doesn't seem to be working.

The original text file I have has something similar to this... these are only a few lines from the full text file, there are several, but these are the ones similar to the ones I want to isolate and work with..


AB*GO1**21.00*etc...~

LO1**1*PQ*1.05**RS*etc..~

LO1**100*PQ*2.00**RS*etc..~

LO1**10*PQ*20.00**RS*etc..~

I want the output to be something like this.

Subtotal: 21.00
Total: (this is the calculated amount in the script)

I must have something wrong since in the subtotal I simply want to display the number after GO1 and it's not any calculation..
0
 
becraigCommented:
Ok so what do you need to do with the LOI lines ?

Are you multiplying the first numerical value by the second ?
0
 
100questionsAuthor Commented:
Yes,
So for instance it would multiply, 1 x 1.05, and then 100 x 2.00, then 10 x 20.00 and it would give me a total.
However for the Subtotal I just simply want it to show the number after G01, which in this case theoretically is 21.00 ( I know it's incorrect, but it's just an example).
0
 
becraigCommented:
Not sure if this is what you are after:
$InputFile = "C:\Data\original.txt"
$OutputFile = "NewData.txt"

[double]$Total = 0
($Input = Get-Content -Path $InputFile) | ? {$_ -match "LO1"} | % {$col=$_.split("*");$total += [double]$col[2] * [double]$col[4]}

$Totline = "Total: $Total"
write-host "This is Total $total"

[double]$Sub = 0
($Input = Get-Content -Path $InputFile) | ? {$_ -match "GO1"} | % {
      $Columns = $_.Split("*")
      $Sub += [double]($Columns[3])
      write-host "This is sub $sub"
}
$subline = "Subtotal $Sub"
@"
$totline 
$subline
"@ | out-file $OutputFile -Encoding UTF8

Open in new window

0
 
100questionsAuthor Commented:
Thanks.  This does not seem to be working correctly.  It's pulling the data in the 3rd column for the subtotal.
Perhaps one last clarification and addition please.  My fault since I have put in the incorrect information for one of the items..

Please see the revised data below that I want to pull info for, please note these lines are scattered within the data and not all together as shown here.  Also note, the line with GO only has 1 * and not 2 * as dividers.
...  
AB*GO*21.00*etc...~
LO1**1*PQ*1.05**RS*etc..~
P1*TS*Company~
LO1**100*PQ*2.00**RS*etc..~
LO1**10*PQ*20.00**RS*etc..~
...

The output I am hoping for is

Name:  Company
Subtotal: $21.00 which is the amount pulled from above
Total:  whatever is calculated from above
0
 
becraigCommented:
Here is a question:
Will there be multiple companies in a single file ?

The code I gave you works just fine for me.

Also your initial file had a match for $_ -match "GO1"

You new file has no "1"
0
 
100questionsAuthor Commented:
Only 1 company per file.
0
 
becraigCommented:
So this works for me based on your file:
$InputFile = "C:\Data\original.txt"
$OutputFile = "NewData.txt"
Get-Content -Path $InputFile | ? {$_ -match "P1"} | % {$col=$_.split("*");$company = $col[2]}
[double]$Total = 0
($Input = Get-Content -Path $InputFile) | ? {$_ -match "LO1"} | % {$col=$_.split("*");$total += [double]$col[2] * [double]$col[4]}
$Totline = "{0:c2}" -f $Total

[double]$Sub = 0
($Input = Get-Content -Path $InputFile) | ? {$_ -match "GO"} | % {
      $Columns = $_.Split("*")
      $sub1 = [double]$Columns[2]
      $subline = "{0:c2}" -f $sub1
}
@"
Company Name:$company
The Total is: $totline 
The subtotal is: $subline
"@ | out-file $OutputFile -Encoding UTF8

Open in new window


File output from my run:
Company Name:Company~
The Total is: $401.05
The subtotal is: $21.00
0
 
100questionsAuthor Commented:
Thanks, however the script is not picking up the company name "Company" by looking through the script.
Company is where the name of a company could be etc..
0
 
becraigCommented:
Ok so just to be clear, you do not want to parse for company from the file, you will provide this value yourself when running the script  ?
0
 
100questionsAuthor Commented:
My apologies, I just noticed it was there.. .Sorry, it works. Many thanks.
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now