0

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.

somebadhat
  • 744
  • 1
  • 5
  • 17

2 Answers2

1

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
Olaf
  • 4,690
  • 2
  • 15
  • 23
  • My original post asked the output to be sent to a text file. So we stay on the same page, how would you do that? – somebadhat Feb 15 '19 at 13:57
  • I do not see this request. But anyway - if you like the output send to a file use `Out-File`. – Olaf Feb 15 '19 at 14:03
  • As this question is written your solution works. It does not work on my real file. See the solution at https://stackoverflow.com/questions/54737787/powershell5-iterate-a-backed-up-ascii-text-file-find-all-instances-of-line2-1 – somebadhat Feb 17 '19 at 21:59
1

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
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
  • Windows-10 desktop. PowerShell 5. As written Results:`cba 1266 1`. Expected Results: `abc LINE2 2`. `gc test.txt` in a new PS window I get: `Method invocation failed because [System.Boolean] does not contain a method named 'Split'.` In a new PS window run as written then run gc test.txt no "Method invocation failed" – somebadhat Feb 15 '19 at 13:47
  • @somebadhat - the code i posted - exactly as written - will work in any ps5.1 session on any OS that has be upgraded to ps5.1 - and win10 should _always_ be up-to-date. what is the exact PoSh version on your test system when you run `$PSVersionTable.PSVersion.ToString()`? i get `5.1.14409.1018` on my win7ps5.1 setup. – Lee_Dailey Feb 16 '19 at 00:12
  • 5.1.17134.590. I've solved my problem. You need not work on it anymore. – somebadhat Feb 17 '19 at 21:10
  • @somebadhat - kool! glad to know that you got it working as needed ... [*grin*] – Lee_Dailey Feb 17 '19 at 21:45