trying to match lines and assign to variable

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 13 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
bebeto
Posts: 8
Last visit: Wed Jun 30, 2010 7:13 am

trying to match lines and assign to variable

Post by bebeto »

Hello,

Let's see if someone can help me out with this powershell script I am trying to write..
I have a $variable containing hundreds of lines (below you can see an example how it looks like)

I need to save different lines in different variables following some pattern like this.
(white spaces) + MPS + lpgp*

(white spaces) + MPS + lpnz*

(white spaces) + MPS + lpgb* ...

I am trying to do something like
$variable | % { if ($_ -match "MPS LPGP*") {$lpgp += $_ }}

But it doesnt work

.....
MPS lpgpctx0001 MPS MPS_PLT_2005.0427 (v1.0) (lpgpctxli0002/27000 30705), start Tue 6/29 15:12 MPS lpnzctx0001 MPS MPS_PLT_2005.0427 (v1.0) (lpgpctxli0002/27000 30705), start Tue 6/29 15:12....
Users of MPS_ENT_CCU: (Total of 10 licenses issued; Total of 0 licenses in use)

Thank you!
User avatar
bebeto
Posts: 8
Last visit: Wed Jun 30, 2010 7:13 am

trying to match lines and assign to variable

Post by bebeto »

Hello,

Let's see if someone can help me out with this powershell script I am trying to write..
I have a $variable containing hundreds of lines (below you can see an example how it looks like)

I need to save different lines in different variables following some pattern like this.
(white spaces) + MPS + lpgp*

(white spaces) + MPS + lpnz*

(white spaces) + MPS + lpgb* ...

I am trying to do something like
$variable | % { if ($_ -match "MPS LPGP*") {$lpgp += $_ }}

But it doesnt work

.....
MPS lpgpctx0001 MPS MPS_PLT_2005.0427 (v1.0) (lpgpctxli0002/27000 30705), start Tue 6/29 15:12 MPS lpnzctx0001 MPS MPS_PLT_2005.0427 (v1.0) (lpgpctxli0002/27000 30705), start Tue 6/29 15:12....
Users of MPS_ENT_CCU: (Total of 10 licenses issued; Total of 0 licenses in use)

Thank you!
User avatar
cruisader03
Posts: 16
Last visit: Tue Mar 15, 2011 2:44 am

trying to match lines and assign to variable

Post by cruisader03 »

the first number in substring is the char to start from counting from 0the second number is how many chars to include$variable | % { New-Object PSObject -Property @{ ColumnA = $_.Substring(0,3) ColumnB = $_.Substring(4,11) ColumnC = $_.Substring(16,3) ... ... ... }}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

trying to match lines and assign to variable

Post by jvierra »

crusader - yes that is a much easier way to state the collection of fields. I forgot about the PSObject constructor frm a hash.

Good call.

User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

trying to match lines and assign to variable

Post by jhicks »

Hmm..I interpreted the problem differently. I thought the problem is one of parsing a text file and getting counts based on different matches.$a=0$b=0$c=0$d=get-content file.txt$d | foreach { Switch -rexeg ($_) { "MPS lpgp" {$a++ } "MPS lpnz" {$b++ } "MPS lpgb" {$c++ } default {"no match for $_"} }}write-host "lpgp count is $a"write-host "lpnz count is $b"write-host "lpgb count is $c"
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

trying to match lines and assign to variable

Post by jvierra »

I think we are all wrong. Looks like a sort issue. Wish we had more info.

In any case teh fields solution will work for any and all problems but if it is a count issue then I suppose Jeff's solution is most elegant.
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

trying to match lines and assign to variable

Post by jhicks »

Regardless of how you get there you end with with four variables that contain numbers. $a=20$b=33$c=50$d=45The best approach then to work with them is to create a custom object, which I believe was suggested earlier.new-object psobject -Property @{ ProductA=$a ProductB=$b ProductC=$c ProductD=$d}You can pipe this object to the Out-Chart cmdlet, which I'm assuming is the one from PowerGadgets.
This topic is 13 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked