简体   繁体   中英

PowerShell index[0] to the first instance of the string, index[1] to the second instance and so on till finished

For example, replace LINE2 1243 with LINE2 1 because it is on line 1 of test.txt.

# Find the line number: 
$lines = sls "LINE2" test.txt | Select-Object -ExpandProperty LineNumber

test.txt:

abc LINE2 1243
lmn LINE2 1250
xyz LINE2 1255

Using:

gc test.txt | % { $_ -replace "LINE2.*", "LINE2 $lines" }

I get:

abc LINE2 1 2 3
lmn LINE2 1 2 3
xyz LINE2 1 2 3

How do I supply index[0], and only index[0], to the first instance of the string, index[1] to the second instance and so on till finished.

Doing it another way:

foreach ($line in $lines){
gc test.txt | % { $_ -replace "LINE2.*", "LINE2 $line" }
}

I get:

abc LINE2 1
lmn LINE2 1
xyz LINE2 1
abc LINE2 2
lmn LINE2 2
xyz LINE2 2
abc LINE2 3
lmn LINE2 3
xyz LINE2 3

How do I get index[0] to only the first instance of the string and so on.

You could use a for loop with an index to achieve this (If I got you right) ;-)

$lines = Select-String "LINE2" -Path C:\sample\test.txt | Select-Object -ExpandProperty LineNumber
Get-Content -Path C:\sample\test.txt -OutVariable Content

for ($index = 0; $index -lt $lines.count; $index++) {
    $Content[$index] -replace "LINE2.*", "LINE2 $($lines[$index])"
}

Output:

abc LINE2 1
lmn LINE2 2
xyz LINE2 3

this is a somewhat different way to do things. [ grin ] what is does ...

  • reads in the file
    i faked this with a here-string, but use Get-Content when doing this for real.
  • gets the matching lines
    it uses the way that -match works against a collection to pull the lines that match the target.
  • splits on the spaces
  • selects the 1st two items from that array
  • adds a $Counter to the collection
  • joins the three items with a space delimiter
  • sends the resulting line to the $Results collection
  • shows that collection on screen
  • saves it to a text file

here's the code ...

# fake reading in a text file
#    in real life, use Get-Content
$InStuff = @'
cba line1 1234
abc LINE2 1243
mnl line4 1244
lmn LINE2 1250
zyx line9 1251
xyz LINE2 1255
qwe line9 1266
'@ -split [environment]::NewLine

$Target = 'Line2'

$Counter = 1
$Results = foreach ($IS_Item in ($InStuff -match $Target))
    {
    $IS_Item.Split(' ')[0..-1] + $Counter -join ' '
    $Counter ++
    }

# on screen
$Results

# to a file
$Results |
    Set-Content -LiteralPath "$env:TEMP\somebadhat.txt"

on screen ...

abc 1243 1
lmn 1250 2
xyz 1255 3

in the text file ...

abc 1243 1
lmn 1250 2
xyz 1255 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM