0

I have a large file with thousands of simple div tags, which I need to index somehow, best by counting up <div n="00001">...<div n="00002">.......<div n="32002">.

So, my question is how to get an array into the basic replace command:

(Get-Content c:\Texts\mytext.txt).replace('<div>', '<div n="$a">') | Set-Content c:\Texts\mytextneu.txt

It would be even better if the resulting id always has 5 digits, as in the example above.

2
  • 1
    Will you potentially have more than one <div> tag on a single line? Commented Sep 2, 2016 at 22:17
  • There are some, but not necessarily so. I could prepare the document in order to avoid it. Commented Sep 4, 2016 at 9:07

1 Answer 1

3

Read the entire file into a string and use Regex.Replace method with a scriptblock replacement:

$text = [IO.File]::ReadAllText('sourcefile.html')
$script:counter = 0
([regex]'<div>').Replace($text, {
    $script:counter++;
    "<div n=`"$('{0:d5}' -f $script:counter)`">"
}) | Out-File output.html -Encoding utf8
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.