Joe
Joe

Reputation: 77

How to remove a first few words from a txt file using powershell

One of the my host.txt file shows a data like below,I want the last word to be exist after trim.

How can I do that ? Can anyone help please ?

| caa3ab95-ecad-46fe-9905-ceac58853ffc | Test-1-my_instance-25ghikbbpip6 |

I want only the name from "Test-1-my_instance-25ghikbbpip6"

I tried below methord but didn't work at all

$hosts = Get-Content C:\host.txt

foreach ($line in $hosts)
    {
    $split1 = $line.trim("|")
    $split2 = $split1.Split(",")[1]
    echo $split2 >> C:\instance.txt
    }

Upvotes: 1

Views: 642

Answers (1)

Will Webb
Will Webb

Reputation: 805

The Following:

$hosts = Get-Content "C:\host.txt"

foreach ($line in $hosts)
    {
        $split1 = $line.trim("|")
        $split2 = $split1.Split("|")[1]
        Write-Output $split2 >> "C:\instance.txt"
    }

Will give an output of:

Test-1-my_instance-25ghikbbpip6

Example:

    PS C:\Windows\system32> 
$hosts = Get-Content "C:\Users\me\Desktop\test.txt"

foreach ($line in $hosts)
    {
        $split1 = $line.trim("|")
        $split2 = $split1.Split("|")[1]
        Write-Output $split2 >> C:\instance.txt
    }

PS C:\Windows\system32> Get-Content C:\instance.txt
 Test-1-my_instance-25ghikbbpip6 

Upvotes: 1

Related Questions