want to select 3rd word of a single string and put it into a variable.

this is the code i have written.

can anybody modify this and give a hint …

foreach($j in Get-Content lun.txt)
{
$vn_name=Get-Content luns.txt | Select-String $j | %{ $_ -split(’’)[0]; }

lun.txt

lunname1
lunname2
lunname3

luns.txt

volname1 lunname1 2gb 1gb
volname2 lunname2 3gb 2gb
volname3 lunname3 4gb 3gb

the output should be 2gb

Here are two ways to do this:

#use regex to capture the 3rd word
(Select-String -Path  luns.txt -Pattern '(?:\w+\s+){2}(\w+)').Matches | ForEach-Object { $_.Groups[1].Value }
#split each line by space and get the 3rd word
Get-Content -Path  luns.txt | ForEach-Object { $_.Split(' ')[2] }

First file lun.txt is not needed.
luns.txt

volname1 lunname1 2gb 1gb
volname2 lunname2 3gb 2gb
volname3 lunname3 4gb 3gb
volname4 lunname1 2gb 1gb
volname5 lunname2 3gb 2gb
volname6 lunname3 4gb 3gb

Code:

# Script to return lun summary

[CmdletBinding(ConfirmImpact='Low')]
Param([Parameter()][ValidateScript({Test-Path $_})][String]$VolumeFilePath = '.\luns.txt')

$Lines = Get-Content $VolumeFilePath

$Volumes = $Lines | % {    
    [PSCustomObject]@{
        VolumeName = $_.Split(' ')[0]
        LunName    = $_.Split(' ')[1]
        SizeGB     = [Int32]($_.Split(' ')[2].SubString(0,1))
        FreeGB     = [Int32]($_.Split(' ')[3].SubString(0,1))
    }
}
# $Volumes | select VolumeName,LunName,SizeGB,FreeGB

$LunList = $Lines | % { $_.Split(' ')[1] } | select -Unique
$Luns = foreach ($LunName in $LunList) {
    $LunVolumes = $Volumes | ? { $_.LunName -eq $LunName } 
    [PSCustomObject]@{
        LunName = $LunName
        SizeGB  = ($LunVolumes | measure SizeGB -Sum).Sum
        FreeGB  = ($LunVolumes | measure FreeGB -Sum).Sum
    }
}
$Luns | select LunName,SizeGB,FreeGB

Output:

LunName  SizeGB FreeGB
-------  ------ ------
lunname1      4      2
lunname2      6      4
lunname3      8      6
[array]$luns = "volname1 lunname1 2gb 1gb","volname2 lunname2 3gb 2gb","volname3 lunname3 4gb 3gb"
foreach($lun in $luns){
  $vn = $lun.split(" ")
  $vn[2]

}

How about…

Get-Content luns.txt | Select-String (get-content lun.txt) | %{ -split $_ | select -index 2 }

or

Get-Content luns.txt | Select-String (get-content lun.txt) | %{ (-split $_)[2] }

Hi js,

thanks for the reply but i am not getting my desired output.

let me reiterate my concern again

for all your codes i am getting the below output…

Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname1 volname2 volname3/lunname1 -off
Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname1 volname2 volname3/lunname2 -off
Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname1 volname2 volname3/lunname3 -off

but i am supposed to get as below.
correct output.
New-NcLun -VserverContext abc -Path /vol/volname1/lunname1 -Size 1gb -Unreserved
Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname1/lunname1 -off
New-NcLun -VserverContext abc -Path /vol/volname2/lunname2 -Size 2gb -Unreserved
Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname2/lunname2 -off
New-NcLun -VserverContext abc -Path /vol/volname3/lunname3 -Size 3gb -Unreserved
Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname3/lunname3 -off

this is my piece of code.

foreach($j in Get-Content lun.txt)
{
$vn_name=get-content luns.txt | ForEach-Object{ $.split(’ ')[0] }
$lun_size=get-content luns.txt | ForEach-Object{ $
.split(’ ')[2] }
echo “New-NcLun -VserverContext abc -Path /vol/$vn_name/$j -Size $lun_size -Unreserved”
echo “Set-NcLunSpaceReserved -VserverContext abc -path /vol/$vn_name/$j -off”
}

need to get single word of each line of luns.txt which works in the below shell command perfectly or anybody knows how i can run the bash scripts in powershell.

for j in `cat /tmp/lun.txt`
do
vn_name=`cat /tmp/luns.txt | grep $j | awk '{print $1}'`
lun_size=`cat /tmp/luns.txt | grep $j | awk '{print $4}'`
#echo “Creating lun,$j using volume,$vn_name of size $lun_size …”
echo “New-NcLun -VserverContext gbcbaffsvm01 -Path /vol/$vn_name/$j -Size $lun_size -Unreserved”
echo “Set-NcLunSpaceReserved -VserverContext gbcbaffsvm01 -path /vol/$vn_name/$j -off”
#echo “Add-NcLunMap -VserverContext gbcbaffsvm01 /vol/$vn_name/$j $iGroup”
printf “\n”
done

Ok, I put select-string back in so you only get one result per variable instead of 3. Select-string puts the original string in the line property.

foreach($j in Get-Content lun.txt)
{
$vn_name=get-content luns.txt | select-string $j | ForEach-Object{ $.line.split(’ ')[0] }
#“vn_name is $vn_name”
$lun_size=get-content luns.txt | select-string $j | ForEach-Object{ $
.line.split(’ ')[2] }
#“lun_size is $lun_size”
echo “New-NcLun -VserverContext abc -Path /vol/$vn_name/$j -Size $lun_size -Unreserved”
echo “Set-NcLunSpaceReserved -VserverContext abc -path /vol/$vn_name/$j -off”
}

thanks for the quick reply and its working quite correctly.

i got to know another way by making my text file into CSV and importing that is making my script a little bit simpler as i don’t need to create any other small files of volume or luns out of my luns.txt.

$volname = Import-Csv luns.txt -Delimiter " " -Header @(“volume”,“lun”,“volsize”,“lunsize”) | ForEach-Object{$_.volume }

is giving the output

volname1
volname2
volname3

Do have the hint to modify the above command and to get a single object in the column ie volname1

Am I stupid …or drunk…, but if you need only the first line then

$volname = Import-Csv luns.txt -Delimiter " " -Header @("volume","lun","volsize","lunsize") | select -first 1 | select -expand volume

What is it actually what you’re trying to achieve? Did you see my answer to your post on the MS Technet Forums?

want to select any word of a single string from a file while comparing the other file and put it into a variable.

Btw I would prefer the -split operator over the string .split() .net method, since it does variable whitespace by default and can do regular expressions:

get-content luns.txt | select-string $j | ForEach-Object{ (-split $_.line)[0] }

I just figured out this way too, but I wouldn’t want to type this all the time:

get-content luns.txt | select-string lunname1 | ForEach-Object{ ([regex]::split($_, “\s+”))[0] }