Solved

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

Posted on 2014-09-10
14
133 Views
Last Modified: 2014-09-11
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
Comment
Question by:100questions
  • 7
  • 7
14 Comments
 
LVL 28

Expert Comment

by:becraig
ID: 40315483
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
 

Author Comment

by:100questions
ID: 40315499
Do you mean to replace Get-Content -Path $OutputFile with your command?
0
 
LVL 28

Expert Comment

by:becraig
ID: 40315503
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
 

Author Comment

by:100questions
ID: 40315546
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
 
LVL 28

Expert Comment

by:becraig
ID: 40315638
Ok so what do you need to do with the LOI lines ?

Are you multiplying the first numerical value by the second ?
0
 

Author Comment

by:100questions
ID: 40315670
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
 
LVL 28

Expert Comment

by:becraig
ID: 40315713
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:100questions
ID: 40316852
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
 
LVL 28

Expert Comment

by:becraig
ID: 40317761
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
 

Author Comment

by:100questions
ID: 40317774
Only 1 company per file.
0
 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
ID: 40317783
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
 

Author Comment

by:100questions
ID: 40317812
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
 
LVL 28

Expert Comment

by:becraig
ID: 40317816
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
 

Author Comment

by:100questions
ID: 40317866
My apologies, I just noticed it was there.. .Sorry, it works. Many thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now