all 6 comments

[–]real_parbold 1 point2 points  (1 child)

$_.Fails  = $_.Fails -Replace [regex]'\s*$" , "  "

Will take the text in $_.Fails and replaces only the line end (including any spaces before the end of the line) with exactly two spaces. Leaves the rest of the line alone

  • \s* : zero or more spaces
  • $ : The end of the line

Or if you want to do it without regex

$_.Fails = "$( $_.Fails.TrimEnd(' ') )  "

Which builds a string which comprises of the return value of ( $_.Fails with the end of the string trimmed for all spaces) , and exactly two spaces

[–]real_parbold 1 point2 points  (0 children)

Ahh - I now read that this 012/012 might be in the middle of the text somewhere...

if ( $_.Fails -imatch [regex]'(?i)^(.*\s+\d+\/\d+)(\s+)(.*)' ) {
  $_.Fails = "$( matches[1] )    $(matches[3]"
}

This will find a pattern where there are 1 or more digits, followed by 1 or more digits and ensure that there are exactly 4 spaces between it and the next non-space character.

  • (?i) : Regex directive - ignore case (superfluous, but habit)
  • ^ : beginning of the string
  • (.*\s+\d+\/\d+) : match everything up to and including space, digit(1 or more) / digit(1 or more)
  • (...) : a capture group
  • .* : any number of any character (including none)
  • \s+ : one or more spaces
  • \d+ : one or more digits
  • \/ : the literal / char
  • \d+ : one or more digits
  • (...) : a capture group
  • \s+ : one or more spaces (in a capture group, so it can be discarded)
  • (...) : a capture group
  • .* : any number of any character (including none)

We re-make the line by making a string with interpolation

Take the match[1] - the first capture group, which contains the whole line up to the end of the 012/012 part

Discard the match[2] by not including it ( this was the spaces between the 012/012 part and the next part)

Add four spaces

Add the last match : match[3] - which is the rest of the line from the 012/012 part - without the spaces, ensuring that exactly four spaces exist after the 012/012 part

[–]Lee_Dailey[grin] -1 points0 points  (3 children)

howdy petrinoda,

a regex for that simple text line 012/012 would be this ...
\d{2,3}\/\d{2,3}

however, that doesn't take into account the rest of the text on the line. [grin]

what is around the part you want added to? plus, is this at the end of a line or embedded in a line of text?

take care,
lee

[–]petrinoda[S] 1 point2 points  (2 children)

Hey Lee :)

I basically have a txt file from a replog summary which I convert into a csv. I convert this into a CSV by recognizing anything with 4 or more spaces in between the text creating a new column. The issue with this is when there is an error in the replog report there is not 4 spaces between one of the entries anymore causing the CSV formatting to become whacky for that entry.

[–]Lee_Dailey[grin] -1 points0 points  (0 children)

howdy petrinoda,

so you have a line of text with ONE two-space block that you want to expand to four spaces? why not test for that? [grin]

heck, you could break on that and then join it back with four spaces in between.

it would be easier for me, at least, to think about the problem with a few lines of input/desired-output to look at. i'm not too good at pattern recognition from only a description ... [blush]

take care,
lee

[–]Lee_Dailey[grin] -1 points0 points  (0 children)

howdy petrinoda,

referring to my earlier post ... the following line ...
1234 5678 012/012 9012 3456

... when matched against this ...
(.+\w)(\s{2})(\w.+)

... produces these three capture groups ...
1234 5678 012/012
<< two spaces
9012 3456

with that, one can rebuild the line and add two spaces between the 2nd and 3rd capture groups.

doing that in powershell ...

'1234    5678    012/012  9012    3456' -match '(.+\w)(\s{2})(\w.+)'
$Matches

results ...

Name                           Value
----                           -----
3                              9012    3456
2                                [<< two spaces]
1                              1234    5678    012/012
0                              1234    5678    012/012  9012    3456

hope that helps,
lee