Masking Sentences

%3CLINGO-SUB%20id%3D%22lingo-sub-3123940%22%20slang%3D%22en-US%22%3EMasking%20Sentences%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3123940%22%20slang%3D%22en-US%22%3E%3CP%3Ewhat%20is%20the%20best%20way%20to%20get%20the%20result%20of%26nbsp%3B%20a**%20d**%20g**%20from%20input%20of%20'abc%20def%20ghi'%3F%20with%20the%20least%20performance%20hit.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24test%20%3D%20'abc%20def%20ghi'%3C%2FP%3E%3CP%3E(((%24test%20-split%20'%20')).substring(0%2C1)%20)%20-join%20'%20'%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Enot%20sure%20how%20to%20add%20the%20**%20or%20even%20a%20single%20*%20will%20be%20good.%20I%20need%20to%20do%20this%20to%20quite%20a%20big%20data%2C%20so%20I%20need%20it%20to%20run%20as%20fast%20as%20you%20can%20without%20using%20the%20%7C%20on%20each%20value...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3123940%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3133515%22%20slang%3D%22en-US%22%3ERe%3A%20Masking%20Sentences%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3133515%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1148418%22%20target%3D%22_blank%22%3E%40dkrabo%3C%2FA%3E%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EOk%2C%20created%20a%20file%20containing%20this%20to%20test%20on%20(d%3A%5Ctemp%5Cx.txt%20in%20my%20case)%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eabc%20def%20ghi%3CBR%20%2F%3Ejkl%20mno%20pqr%3CBR%20%2F%3Estu%20vwx%20yz%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20I%20let%20this%20script%20run%20on%20it%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eforeach%20(%24line%20in%20get-content%20d%3A%5Ctemp%5Cx.txt)%20%7B%0A((((%24line%20-split%20'%20')).substring(0%2C1)%20)%20-join%20'%20').Replace('%20'%2C'**%20')%20%7C%20foreach%20%7B%24_%20%2B%20'**'%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThis%20is%20the%20output%3A%3CBR%20%2F%3Ea**%20d**%20g**%3CBR%20%2F%3Ej**%20m**%20p**%3CBR%20%2F%3Es**%20v**%20y**%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

what is the best way to get the result of  a** d** g** from input of 'abc def ghi'? with the least performance hit.

 

$test = 'abc def ghi'

((($test -split ' ')).substring(0,1) ) -join ' '

 

not sure how to add the ** or even a single * will be good. I need to do this to quite a big data, so I need it to run as fast as you can without using the | on each value...

4 Replies

@dkrabo 

Ok, created a file containing this to test on (d:\temp\x.txt in my case) 

 

abc def ghi
jkl mno pqr
stu vwx yz

 

If I let this script run on it:

foreach ($line in get-content d:\temp\x.txt) {
(((($line -split ' ')).substring(0,1) ) -join ' ').Replace(' ','** ') | foreach {$_ + '**'}
}

This is the output:
a** d** g**
j** m** p**
s** v** y**

@dkrabo  the solution from @Harm_Veenstra seems pretty optimised, testing on a 10,000 line text file it masks all values in about 2s when part of a pipeline.

Didn't even test it in that amount of data :)
well, as you had answered the question there wasnt much else for me to do ! :)