Capture second word in a line

by dragonfly26 at 2013-02-24 05:11:39

Hi,
I have an output file were i only need to capture the second word of each line.
example:
Removed windows6 on esx01
I only need to caputre the word "windows6" in this example.
by dragonfly26 at 2013-02-24 06:35:31
I’ve solved it myself by using the following.

## Get content of txt file with multiple lines
$lines = Get-Content c:\demo\lines.txt

foreach ($_ in $lines){
$out = $_.split()
$out[1]

}

if someone can tell me how to do this with using regular expression, I am very intrested.
by mjolinor at 2013-02-24 07:39:53
One way:
(Get-Content c:\demo\lines.txt -r 0) -replace '^\s*\S+\s(\S+)\s.+$','$1'
by nohandle at 2013-02-24 12:35:05
As I said on another forum I’d use -SPLIT " ",3 space as word separator and split only on first two occurances. But you could also use \b (word boundary) in similar manner.
by mjolinor at 2013-02-24 13:25:22
If you use -split, you have to foreach through the array. The -replace operator will do the whole array at once.
by dragonfly26 at 2013-02-25 00:19:12
Thanks guys for the helpful answers