RegEx Query

Hi, I have the following text file:

root@uuupsapi12(SAP:PRD)# echo | format | grep 3PAR | grep A057d0 # DCA Disks
5. c0t60002AC000000000000005A00001A057d0 <3PARdata-VV-3315-101.00GB>
6. c0t60002AC000000000000005A10001A057d0 <3PARdata-VV-3315-101.00GB>
7. c0t60002AC000000000000005A20001A057d0 <3PARdata-VV-3315-101.00GB>
8. c0t60002AC000000000000005A30001A057d0 <3PARdata-VV-3315-101.00GB>
9. c0t60002AC000000000000005A40001A057d0 <3PARdata-VV-3315-101.00GB>
10. c0t60002AC000000000000005A50001A057d0 <3PARdata-VV-3315-101.00GB>
11. c0t60002AC000000000000005A70001A057d0 <3PARdata-VV-3315-101.00GB>
21. c0t60002AC0000000000000058E0001A057d0 <3PARdata-VV-3315-101.00GB>
22. c0t60002AC0000000000000058F0001A057d0 <3PARdata-VV-3315-101.00GB>

And only want to extract the data from 60002a to d0: 60002AC0000000000000058F0001A057d0, for example - from every line and extract it to another text file:

I have generated the following RegEx query (very basic I know!)
‘/60002AC\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d?../g’

And therefore wrote the following code:

$olddata = “H:\Documents\Scripts\Server Graph\unix-email.txt”
$destination_file = ‘H:\Documents\Scripts\Server Graph\unix-new.txt’
$regex = ‘/60002AC\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d?../g’
$olddata | Select-String -Pattern $regex -AllMatches | % {$.Matches} | % {$.Value} > $destination_file

But it doesn’t give me anything in the output file, any help will be much appreciated!

A couple things I see right away. When you pipe $olddata to Select-String, you are literally piping the path not the contents. You would need to either use the Get-Content cmdlet or create a custom object with a property specifically called “literalpath” with the string to the path as the value, then pipe it to Select-String. I’m not sure that your regular expression is correct either, but there is a much simpler way to accomplish what you are trying to achieve. Here’s what I would do:

Get-Content $old |
    ForEach-Object {$_ -replace "<.*>", ""} |
        Set-Content $new

 

Hi Mike,

Thanks so much for your help!, I have migrated your code and now get the output below. I tried to run your regex syntax via the Regex101 website but doesn’t seem to work. Can you provide any help or tips to amend your regex to show the desired output below:

Output

root@uuuservername(SAP:PRD)# echo | format | grep 3PAR | grep A057d0 # DCA Disks
5. c0t60002AC000000000000005A00001A057d0
6. c0t60002AC000000000000005A10001A057d0
7. c0t60002AC000000000000005A20001A057d0
8. c0t60002AC000000000000005A30001A057d0
9. c0t60002AC000000000000005A40001A057d0
10. c0t60002AC000000000000005A50001A057d0
11. c0t60002AC000000000000005A70001A057d0
21. c0t60002AC0000000000000058E0001A057d0
22. c0t60002AC0000000000000058F0001A057d0
23. c0t60002AC0000000000000059A0001A057d0 # 101.00GB
24. c0t60002AC0000000000000059B0001A057d0 # 101.00GB
25. c0t60002AC0000000000000059C0001A057d0
26. c0t60002AC0000000000000059D0001A057d0

Desired Output

60002AC000000000000005A00001A057d0
60002AC000000000000005A10001A057d0
60002AC000000000000005A20001A057d0
60002AC000000000000005A30001A057d0
60002AC000000000000005A40001A057d0
60002AC000000000000005A50001A057d0
60002AC000000000000005A70001A057d0
60002AC0000000000000058E0001A057d0
60002AC0000000000000058F0001A057d0
60002AC0000000000000059A0001A057d0
60002AC0000000000000059B0001A057d0
60002AC0000000000000059C0001A057d0
60002AC0000000000000059D0001A057d0

 

If it always starts with ‘c0t’ and is followed by ’ <’ then then you can use this simple regex replace.

$text = @'
c0t60002AC000000000000005A00001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A10001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A20001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A30001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A40001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A50001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A70001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC0000000000000058E0001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC0000000000000058F0001A057d0 <3PARdata-VV-3315-101.00GB>
'@

$text -replace 'c0t(.+?) <.+','$1'

Output

60002AC000000000000005A00001A057d0
60002AC000000000000005A10001A057d0
60002AC000000000000005A20001A057d0
60002AC000000000000005A30001A057d0
60002AC000000000000005A40001A057d0
60002AC000000000000005A50001A057d0
60002AC000000000000005A70001A057d0
60002AC0000000000000058E0001A057d0
60002AC0000000000000058F0001A057d0

And this will work on a single string as shown or a string array.

Thanks Doug! That’s nearly got it! - I have amended your RegEx to:

-replace ‘…c0t(.+?) <.+’,’$1’

This now gives me the output below, all I need to do now is remove any text, lines and spaces that do not contain

60002AC00000000000000xxxxxxxxxxxxxxx

Is this possible?

Actual Output

root@serversapi12(SAP:PRD)# echo | format | grep 3PAR | grep A057d0 # DCA Disks
60002AC000000000000005A00001A057d0
60002AC000000000000005A10001A057d0
60002AC000000000000005A20001A057d0
60002AC000000000000005A30001A057d0
60002AC000000000000005A40001A057d0
60002AC000000000000005A50001A057d0
60002AC000000000000005A70001A057d0
60002AC0000000000000058E0001A057d0
60002AC0000000000000058F0001A057d0
60002AC0000000000000059A0001A057d0
60002AC0000000000000059B0001A057d0
60002AC0000000000000059C0001A057d0
60002AC0000000000000059D0001A057d0
60002AC0000000000000059E0001A057d0
60002AC0000000000000059F0001A057d0
60002AC000000000000005900001A057d0
60002AC000000000000005910001A057d0
60002AC000000000000005920001A057d0
60002AC000000000000005930001A057d0
60002AC000000000000005940001A057d0
60002AC000000000000005950001A057d0
60002AC000000000000005960001A057d0
60002AC000000000000005990001A057d0
60002AC000000000000006170001A057d0
root@server(SAP:PRD)#
root@server(SAP:PRD)# echo | format | grep 3PAR | grep E726d0 # DCB Disks
60002AC000000000000003EE0001E726d0
60002AC0000000000000036A0001E726d0
60002AC0000000000000036B0001E726d0
60002AC0000000000000036C0001E726d0
60002AC0000000000000036D0001E726d0
60002AC0000000000000036E0001E726d0
60002AC0000000000000036F0001E726d0
60002AC0000000000000037A0001E726d0
60002AC0000000000000037D0001E726d0
60002AC0000000000000037E0001E726d0
60002AC000000000000003650001E726d0
60002AC000000000000003660001E726d0
60002AC000000000000003670001E726d0
60002AC000000000000003680001E726d0
60002AC000000000000003690001E726d0
60002AC000000000000003700001E726d0
60002AC000000000000003710001E726d0
60002AC000000000000003720001E726d0
60002AC000000000000003730001E726d0
60002AC000000000000003740001E726d0
60002AC000000000000003750001E726d0
60002AC000000000000003760001E726d0
60002AC000000000000003770001E726d0
60002AC000000000000003780001E726d0
60002AC000000000000003790001E726d0

Desired Output

60002AC000000000000005A00001A057d0
60002AC000000000000005A10001A057d0
60002AC000000000000005A20001A057d0
60002AC000000000000005A30001A057d0
60002AC000000000000005A40001A057d0
60002AC000000000000005A50001A057d0
60002AC000000000000005A70001A057d0
60002AC0000000000000058E0001A057d0
60002AC0000000000000058F0001A057d0
60002AC0000000000000059A0001A057d0
60002AC0000000000000059B0001A057d0
60002AC0000000000000059C0001A057d0
60002AC0000000000000059D0001A057d0
60002AC0000000000000059E0001A057d0
60002AC0000000000000059F0001A057d0
60002AC000000000000005900001A057d0
60002AC000000000000005910001A057d0
60002AC000000000000005920001A057d0
60002AC000000000000005930001A057d0
60002AC000000000000005940001A057d0
60002AC000000000000005950001A057d0
60002AC000000000000005960001A057d0
60002AC000000000000005990001A057d0
60002AC000000000000006170001A057d0
60002AC000000000000003EE0001E726d0
60002AC0000000000000036A0001E726d0
60002AC0000000000000036B0001E726d0
60002AC0000000000000036C0001E726d0
60002AC0000000000000036D0001E726d0
60002AC0000000000000036E0001E726d0
60002AC0000000000000036F0001E726d0
60002AC0000000000000037A0001E726d0
60002AC0000000000000037D0001E726d0
60002AC0000000000000037E0001E726d0
60002AC000000000000003650001E726d0
60002AC000000000000003660001E726d0
60002AC000000000000003670001E726d0
60002AC000000000000003680001E726d0
60002AC000000000000003690001E726d0
60002AC000000000000003700001E726d0
60002AC000000000000003710001E726d0
60002AC000000000000003720001E726d0
60002AC000000000000003730001E726d0
60002AC000000000000003740001E726d0
60002AC000000000000003750001E726d0
60002AC000000000000003760001E726d0
60002AC000000000000003770001E726d0
60002AC000000000000003780001E726d0
60002AC000000000000003790001E726d0

The output I showed is the exact output you show desired? Or what am I missing?

Hi Doug, sorry I might have not explained myself well. If I edit your script, here is an example of the text file I am importing

2
3
4
5
6
7
8
9
10
11
12
13
$text = @'
root@serversapi12(SAP:PRD)# echo | format | grep 3PAR | grep A057d0 # DCA Disks
c0t60002AC000000000000005A00001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A10001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A20001A057d0 <3PARdata-VV-3315-101.00GB>
root@server(SAP:PRD)# root@server(SAP:PRD)# echo | format | grep 3PAR | grep E726d0 # DCB Disks
c0t60002AC000000000000005A30001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A40001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A50001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A70001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC0000000000000058E0001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC0000000000000058F0001A057d0 <3PARdata-VV-3315-101.00GB>
'@
$text -replace 'c0t(.+?) <.+','$1'
So there are various text entries (and white spaces) are included in the import file and would like to ignore (if poss), I would like the RegEx to capture the 60002ACxxxxxxxxxxxxxxxxxxxxxxdo string

Your text output has the prompt and the commands themselves?

Ok assuming this is one string, we will split at the new line characters and process each line one at a time. If this is a string array, you can drop the split portion

$text = @’
root@serversapi12(SAP:PRD)# echo | format | grep 3PAR | grep A057d0 # DCA Disks
c0t60002AC000000000000005A00001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A10001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A20001A057d0 <3PARdata-VV-3315-101.00GB>
root@server(SAP:PRD)#
root@server(SAP:PRD)# echo | format | grep 3PAR | grep E726d0 # DCB Disks
c0t60002AC000000000000005A30001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A40001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A50001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC000000000000005A70001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC0000000000000058E0001A057d0 <3PARdata-VV-3315-101.00GB>
c0t60002AC0000000000000058F0001A057d0 <3PARdata-VV-3315-101.00GB>
‘@

$text -split "`n" | foreach {
    if($_ -match ‘c0t(.+?) <.+’)
    {
        $matches.1
    }
}

Output

60002AC000000000000005A00001A057d0
60002AC000000000000005A10001A057d0
60002AC000000000000005A20001A057d0
60002AC000000000000005A30001A057d0
60002AC000000000000005A40001A057d0
60002AC000000000000005A50001A057d0
60002AC000000000000005A70001A057d0
60002AC0000000000000058E0001A057d0
60002AC0000000000000058F0001A057d0

Doug, that’s awesome!, exactly what I wanted - thanks so much for your help!