Author Posts

May 7, 2014 at 9:02 am

Hello,

I am new with powershell and I need some help with command.

Here is my data:

Diagnostic data directory path (DIAGPATH) = c:\users\db2admin\

What I am want to get the value after the = sign, the output should look like this
c:\users\db2admin\

Here is the command I running to get the data, but I can't figure out how to get c:\users\db2admin\

db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match "ALT_DIAGPATH")}

If ther an option to add to above command to print last value after = sign?

May 7, 2014 at 11:12 am

Hello, you can use the Split method of System.String, ie:

(db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match “ALT_DIAGPATH”)}).Split("=")[1]

and in your case even

(db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match “ALT_DIAGPATH”)}).Split("=")[1].Trim()
^ ^
to get rid of the whitespace (Note the parentheses)

I always feel awkward specifying the array index like that, but if you know there's only one split character, it seems straightforward, maybe someone has a more elegant approach.

http://msdn.microsoft.com/en-us/library/system.string.split.aspx

May 8, 2014 at 5:36 am

Hi Greg,

Thank you for reply. I am getting the following error while excuting the command.

PS C:\Users\db2admin> (db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match "ALT_DIAGPATH")}).Split("=")[
1].Trim()
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] doesn't contain a method named 'Split'.
At line:1 char:93
+ (db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match "ALT_DIAGPATH")}).Split <<<< ("=")[1].Trim() + CategoryInfo : InvalidOperation: (Split:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

May 8, 2014 at 7:20 am

Hi Hamid,

I was actually expecting to see a post of Dave's, since he is a Regex expert 🙂
But maybe I can help you too:


$a = "Diagnostic data directory path (DIAGPATH) = c:\users\db2admin\"
$null = $a -match "(?< == ).*" $Matches.Values

Explanation: first we assign the string to a variable.
Then we do a Regex match: find all characters after "= ". We assign the match to the $null variable, just to avoid the output "True" which tells you that there is a valid match.
At last point, we just call the found matches.

Hope this helps!

May 8, 2014 at 10:44 am

Hi deiandrei and Greg,

Thank you for both of suggestion. I found a solution, it seems very easy:

((db2 get dbm cfg | select-string DIAGPATH) -split ' = ')[1]