Solved

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

Posted on 2014-09-10
14
134 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 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
 

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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

22 Experts available now in Live!

Get 1:1 Help Now