DigDeep Posted May 16, 2018 Posted May 16, 2018 I am sure this might have been posted earlier but doesn't work for me. I have a file as sample.txt and want to delete anything before and after the text @UserName & "{". In the file, @UserName is written as DIGDEEP. I have attached the sample.txt too here. The End Result should be: DIGDEEP{testing-123 {testing-106 ; In the file, DIGDEEP is refered as the User Name Local $FilePath = @DesktopDir & "\sample.txt" $file_count_lines = _FileCountLines($FilePath) for $i = 0 to $file_count_lines $Lines_text_output = FileReadLine($FilePath, $i) Select Case StringInStr($Lines_text_output, @UserName & "{") $OutPut = StringTrimLeft($Lines_text_output, StringInStr($Lines_text_output, "{") - 1) EndSelect Next Any help please? sample.txt
rudi Posted May 16, 2018 Posted May 16, 2018 (edited) Hello and welcome to the forum. StringRegExpReplace() is the function, that will do the job for you. Q: Can there follow multiple lines like ... {testing-nnn ??? Regards, Rudi. Edited May 16, 2018 by rudi Earth is flat, pigs can fly, and Nuclear Power is SAFE!
DigDeep Posted May 16, 2018 Author Posted May 16, 2018 @rudi, thanks for the reply. Yes, there might be multiple lines sometimes. The only fixed text here will be @UserName & "{". But the texts, numbers inside the bracket will always be different, which I was facing issue to determine to delete before and after those texts. Let me also have a look with StringRegExpReplace() once.
iamtheky Posted May 16, 2018 Posted May 16, 2018 is the "Test results completed" part the same everytime? msgbox(0,'' , stringregexp(stringstripws(fileread("sample.txt") , 8) , "(" & @UserName & ".*)Testresultscompleted" , 3)[0]) Reveal hidden contents ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mikell Posted May 16, 2018 Posted May 16, 2018 StringRegExpReplace needs very precise instructions For instance the expression below means : - keep all lines beginning with "{" - delete all text before "DIGDEEP" on the concerned line but keep the rest of the line - delete all other lines $text = FileRead("sample.txt") $res = StringRegExpReplace($text, '(?m)^\{\N+(*SKIP)(*F)|^.*?(?=DIGDEEP)|^.*\R?', "") Msgbox(0,"", $res)
DigDeep Posted May 17, 2018 Author Posted May 17, 2018 First of all, thank you to everyone for providing all different ways. I picked up one of them from here and expanded more. ; In the file, DIGDEEP is refered as the User Name Local $FilePath = @DesktopDir & "\sample.txt" $file_count_lines = _FileCountLines($FilePath) for $i = 0 to $file_count_lines $Lines_text_output = FileReadLine($FilePath, $i) ; Way 1 Select Case StringInStr($Lines_text_output, @UserName & "{") ; If we want the output with username $OutPut = StringTrimLeft($Lines_text_output, StringInStr($Lines_text_output, "{") - 1) ; This will provide result as: DIGDEEP{XXXXXXXXXX EndSelect Next ; Way 2 Select Case StringInStr($Lines_text_output, "{") ; If we want the output of the last line, if there are multiple entries $OutPut = StringTrimLeft($Lines_text_output, StringInStr($Lines_text_output, "{") - 1) ; This will provide result as: {XXXXXXXXXX EndSelect ; Way 3 Select Case StringInStr($Lines_text_output, "{") ; If we want the output of the last line if there are multiple entries $OutPut = StringTrimLeft($Lines_text_output, StringInStr($Lines_text_output, "{") - 1) $OutPut1 = StringTrimLeft($OutPut, 1) ; This will provide Output removing the '{' as: XXXXXXXXXX EndSelect
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now