Solved

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

Posted on 2014-09-10
14
137 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 29

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 29

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 29

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 29

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
 

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 29

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 29

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 29

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

623 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