Jump to content

Delete Before and after String


Recommended Posts

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

Link to post
Share on other sites

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!

Link to post
Share on other sites

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

Link to post
Share on other sites

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

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

 

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

Link to post
Share on other sites

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)

:)

Link to post
Share on other sites

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

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...