Printout only 3rd section of a line

I see a lot of “grab last word”, “grab first/last X characters” or “grab lines that contain XXX” however I’ve been unable to find a solution here for my problem.
For example, my text file contains thousands of lines like:
[0000001] item_title apple_juice_2L_concentrate 0x000581
[0000002] item_description 98%_reconstituted_apple_juice 0x00585
[0000003] item_title orange_juice_2L_natural 0x000679
[0000004] item_description 100%_fresh_squeeze_orange_juice 0x00985
[0000005] item_title apple_juice_1L_natural 0x000429
[0000006] item_description 100%_natural_apple_juice_cloudy 0x00587

how would I achieve a printout like this while cutting out the junk i dont want like the [0000001], item_title and the 0x000581
apple_juice_2L_concentrate
apple_juice_1L_natural

I’m currently using:
Get-Content -Path H:"inventory management"\juice\juices.txt | Where-Object {$_ -like ‘item_title apple_juice’}

which returns
[0000001] item_title apple_juice_2L_concentrate 0x000581
[0000005] item_title apple_juice_1L_natural 0x000429

All I want to do is adjust my code to not printout the entire line as it does now, but just the 3rd section.

Appreciate your time!

The only way is Regex.

Write regex string to match the content.

Instead of like parameter use match parameter to use regex string.

You have to use foreach loop to iterate line by line and match the data. Also to get the desired output you need to create custom object as well.

As Sankhadip suggests, regex is your friend for this type of problem. You should read through Microsoft’s regex documentation page. For this particular instance, you can solve the problem easily with -split.

Here’s an example, modifying your code:

Get-Content -Path H:\"inventory management"\juice\juices.txt | (Where-Object {$_ -like '*item_title apple_juice*'} -split ' ')[2]

What this does is split your output string

[0000001] item_title apple_juice_2L_concentrate 0x000581

into individual strings based on the selected split character(s). In this case, we are splitting the string at spaces, hence

-split ' '

which is actually a very simple regex string. The result is the following strings:

[0000001]
item_title
apple_juice_2L_concentrate
0x000581

which are stored in an array for convenience. The

[2]

at the end selects the third element in the array, which is the output that you want:

apple_juice_2L_concentrate

[quote quote=181746]As Sankhadip suggests, regex is your friend for this type of problem. You should read through Microsoft’s regex documentation page. For this particular instance, you can solve the problem easily with -split. Here’s an example, modifying your code:

[/quote]

Get-Content -Path H:<span class=“ace_string”>“inventory management”<span class=“ace_identifier”>juice<span class=“ace_identifier”>juices.txt | (Where-Object {$_ -like item_title apple_juice} -split ’ ')[2]

This returned this error:

Expressions are only allowed as the first element of a pipeline.

This worked though, thanks anyway:

$InStuff = Get-Content -Path 'H:\inventory management\juice\juices.txt' 

$TargetPhrase = 'item_title'

$Results = $InStuff -match $TargetPhrase |
    ForEach-Object {
        $_.Split(' ')[2]
        }

$Results -match 'apple_juice'