1

I've got several text files with lots of un-needed information in. I essentially want to read each line of the file and output everything after a certain phrase on each line to a text file eg:

Some rubbish not needed blah blah SPLITPHRASE Some useful info

So basically I want to go through each line and extract everything after SPLITPHRASE on each line. N.B The text file only contains spaces as delimiters

So far I've experimented with .substring, .linesplit, converting to CSV and many other things. I get the feeling it's probably going to be a really simple solution, but for some reason I seem to have just hit a brick wall!! Any help much appreciated

Edit: Also the length of the string before and after SPLITPHRASE is not fixed

1 Answer 1

2

like this?

$a = "Some rubbish not needed blah blah SPLITPHRASE Some useful info","Some rubbish not needed  SPLITPHRASE usefull" # can be $a = get-content .\myfile.txt

$a | select-string -patter '(?<=SPLITPHRASE)(.*)'  | 
select -expa matches | select -expa value |  % { $_.trim() }

Result:

Some useful info
usefull
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.