Powershell replace function - replacing lines with numbers

Remy van Tour

Basicly I am trying to find a way to put a variable for replacing a line in powershell.

The current script:

$switches = get-outlookinbox | where subject -eq "Hello"
    $e = $switches.body
    $e = $e.replace("Hello:","")
    $e = $e.replace(" Number","")
    $e = $e.replace(":1","")
    $e = $e.replace(":2","")
    $e = $e.replace(":3","")
    $e = $e.replace(":4","")
    $e = $e.replace(":99","")

You can see what I am going for here... But I don't want 99 lines of replace code, any thoughts on this? Also, the numbers MUST have : infront of it, otherwise the replace will corrupt the file since it contains only IP's and ports, it's the ports I want to remove from the output.

user2555451

You can use a simple foreach loop and iterate from 99 to 1:

foreach ($n in 99..1)
{
    $e = $e.Replace(":$n", " ")
}

Or, if you prefer one line:

foreach ($n in 99..1) { $e = $e.Replace(":$n", " ") }

Demo:

PS > $mystr = "a:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20b"  
PS > foreach ($n in 20..1) { $mystr = $mystr.replace(":$n", "") }
PS > $mystr
ab 
PS > 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP's preg_replace() function is not replacing all lines

From Dev

Replacing comma by dot only in numbers in a texteditor using search and replace function

From Dev

Replacing repeated lines of code with a function

From Dev

Javascript replace function not replacing all

From Dev

Replacing lines of a text file with powershell, based on lines of a different text file

From Dev

Replacing specific numbers with String.Replace or Regex.Replace

From Dev

Use a function in Powershell replace

From Dev

Why does removal of empty lines from multiline string in PowerShell fail using Replace function?

From Dev

Replacing more than one elements with replace function

From Dev

Replacing more than one elements with replace function

From Dev

Powershell replace text on lines based on length

From Dev

Replacing several sub-version numbers with another one using PowerShell

From Dev

Haskell - Trying to apply a function to lines of multiple numbers

From Dev

VBA EXCEL : Pattern creation function replacing numbers with characters

From Dev

Issues with replacing words in a string using a dictionary and the replace() function

From Dev

Function to replace list with numbers and signs in python

From Dev

Replacing strings in lines

From Dev

replacing lines in file

From Dev

Replace multiple lines in one file with the same lines at the same line numbers in another file?

From Dev

Python replacing numbers in a list

From Dev

Replacing selective numbers with NaNs

From Dev

Replacing numbers with zeros in golang

From Dev

replacing of numbers to zero in a sum

From Dev

replacing of numbers to zero in a sum

From Dev

Replacing text in vector with numbers?

From Dev

Replacing numbers with letters in string

From Dev

Replacing a word with numbers

From Dev

Replacing some numbers in javascript

From Dev

Replacing ereg_replace()

Related Related

  1. 1

    PHP's preg_replace() function is not replacing all lines

  2. 2

    Replacing comma by dot only in numbers in a texteditor using search and replace function

  3. 3

    Replacing repeated lines of code with a function

  4. 4

    Javascript replace function not replacing all

  5. 5

    Replacing lines of a text file with powershell, based on lines of a different text file

  6. 6

    Replacing specific numbers with String.Replace or Regex.Replace

  7. 7

    Use a function in Powershell replace

  8. 8

    Why does removal of empty lines from multiline string in PowerShell fail using Replace function?

  9. 9

    Replacing more than one elements with replace function

  10. 10

    Replacing more than one elements with replace function

  11. 11

    Powershell replace text on lines based on length

  12. 12

    Replacing several sub-version numbers with another one using PowerShell

  13. 13

    Haskell - Trying to apply a function to lines of multiple numbers

  14. 14

    VBA EXCEL : Pattern creation function replacing numbers with characters

  15. 15

    Issues with replacing words in a string using a dictionary and the replace() function

  16. 16

    Function to replace list with numbers and signs in python

  17. 17

    Replacing strings in lines

  18. 18

    replacing lines in file

  19. 19

    Replace multiple lines in one file with the same lines at the same line numbers in another file?

  20. 20

    Python replacing numbers in a list

  21. 21

    Replacing selective numbers with NaNs

  22. 22

    Replacing numbers with zeros in golang

  23. 23

    replacing of numbers to zero in a sum

  24. 24

    replacing of numbers to zero in a sum

  25. 25

    Replacing text in vector with numbers?

  26. 26

    Replacing numbers with letters in string

  27. 27

    Replacing a word with numbers

  28. 28

    Replacing some numbers in javascript

  29. 29

    Replacing ereg_replace()

HotTag

Archive