Jump to content
DigDeep

Delete Before and after String

Recommended Posts

DigDeep

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

Share this post


Link to post
Share on other sites
rudi

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 by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
DigDeep

@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.

Share this post


Link to post
Share on other sites
iamtheky

is the "Test results completed" part the same everytime?

msgbox(0,'' ,  stringregexp(stringstripws(fileread("sample.txt") , 8) , "(" & @UserName & ".*)Testresultscompleted"  , 3)[0])

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
mikell

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)

:)

Share this post


Link to post
Share on other sites
DigDeep

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

 

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.