0

Original Data :

<tr><td>[email protected]</td><td></td><td></td><td></td></tr>
<tr><td>[email protected]</td><td></td><td></td><td></td></tr>
<tr><td>[email protected]</td><td></td><td></td><td></td></tr>
<tr><td>[email protected]</td><td></td><td></td><td></td></tr>
<tr><td>[email protected]</td><td></td><td></td><td></td></tr>
<tr><td>[email protected]</td><td></td><td></td><td></td></tr>
<tr><td>[email protected]</td><td></td><td></td><td></td></tr>
<tr><td>[email protected]</td><td></td><td></td><td></td></tr>

In File <tr> need to be replace with below list in sequence loop line by line

<tr class="danger">
<tr class="warning">
<tr class="active">
<tr class="success">

Tried :

$str = Get-Content C:\Users\admin\Desktop\n\2.html

#odd
$f=0; $str.Split("`n") | % { if($f = !$f) { $_ } }

#even
$g=1; $str.Split("`n") | % { if($f = !$f) { $_ } }

$f -replace "<tr>", '<tr class="active">'
$g -replace "<tr>", '<tr class="success">'

Result not came as expected

1
  • $f -replace "<tr>", '<tr class="active">' - the variable $f is your odd/even counter, it has no text in it to replace. Commented Aug 19, 2017 at 8:38

1 Answer 1

1

To have four rotational classes count lines and do a modulus division %
and use a switch command.

$file = Get-Content "C:\Users\admin\Desktop\n\2.html"

$cnt=0
ForEach ($line in $file){
    if ($line -match '^\<tr\>') {
        switch ($cnt) {
            0 {$line -replace '\<tr\>','<tr class="danger">'}
            1 {$line -replace '\<tr\>','<tr class="warning">'}
            2 {$line -replace '\<tr\>','<tr class="active">'}
            3 {$line -replace '\<tr\>','<tr class="success">'}
        }
        $cnt++
        $cnt%=4
    } else {
        $line
    }
}

Or put the classes in an array and insert by index:

$file = Get-Content "C:\Users\admin\Desktop\n\2.html"
$classes = ('danger','warning','active','success')
$cnt=0
ForEach ($line in $file){
    if ($line -match '^\<tr\>') {
        $line -replace '\<tr\>', ('<tr class="'+$($classes[$cnt])+'">')
        $cnt++
        $cnt%=4
    } else {
        $line
    }
}
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.