Shell – Remove all characters appearing before a certain string in PowerShell

powershellscript

I have a text file with the following information:

24.;128.32.;128,6;0.128.68.;128,1.;0.76.;;;;;0,1.;0.4.;0,2.;0.5.;0,4.;;;0.76.;128,;;;;;;;;172.30.1.1,172.30.1.228,

I need to remove all characters thatĀ appear before 172.30.1.1.
For my file, it looks like this:

172.16.1.1,172.16.1.210,

I'm using the Poweshell to do this, but without success.

Best Answer

A lot of it will depend on how consistent your data will be. There will be a multitude of methods. You could do it with a split:

$test = '24.;128.32.;128,6;0.128.68.;128,1.;0.76.;;;;;0,1.;0.4.;0,2.;0.5.;0,4.;;;0.76.;128,;;;;;;;;172.30.1.1,172.30.1.228,'
$partIWant = $test.Split(';')[$($test.Split(';').Count-1)]
$partIWant

You could use a regex as well:

$partIWant = $test -replace '.+(\d+.\d+\.\d+\.\d+\.\d+,\d+\.\d+\.\d+\.\d+,)', '$1'
$partIWant

You will need to pick the method that you want to use and craft it to the specific data that you will be working with.

Related Question