0

I need to get part of this file for example, I need extract the following

Main, Branches\Branch1

in one variable also the I cannot have duplicate values

It is possible with powershell?

This is the file:

This is a garbage line
This is another garbage line
c:\Folder\Main\Folder\..\Folder
c:\Folder\Main\Folder\..\Folder
c:\Folder\Branches\Branch1\Folder\..\Folder
c:\Folder\Branches\Branch1\Folder\..\Folder
c:\Folder\Branches\Branch1\Folder\..\Folder
c:\Folder\Main\Folder\..\Folder
c:\Folder\Main\Folder\..\Folder
this is the final line..

2 Answers 2

2

But of course ...

According to the fact $files contain your lines

$files = Get-content "your file"

You can use the following to be sure that there is no duplicate :

$files | Sort-Object -Unique

Then you can use Test-path to be sure that path exists

$files | Sort-Object -Unique | where {Test-Path $_ -ErrorAction SilentlyContinue}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks man, when I put $files | Sort-Object -Unique delete all the util information and just show this:This is a garbage line This is another garbage line this is the final line..
Sorry, I don't understand. Does it answer your question ?
No, I have probhlem with the first line
What is the problem with the garbage line ?
-1

This will extract those values from the sample data using a -like filter to take out the garbage and a -replace to do the extract. The sort -unique will remove the duplicates, but it won't keep the extracted values in the same order they were in the file.

(get-content testfile.txt) -like 'c:\Folder*' -replace 'c:\\Folder\\(.+?)\\Folder.+','$1' |
 sort -unique

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.