Check Position of My Word and Delete Lines before and after it's related line


I have one text file contains many lines, I’m searching for a word inside it, and whenver found I will delete all lines before and after that line, How to achieve this ?

Thanks in advance

Hi @hsendel

First read the text file and store in string varible

Then split the string based on new line as
Stringvariable.Split(Environment.NewLine.TocharArray) will split the lines based on new line and store in array

After that use for each loop and loop through line

Inside that use the if activitiy with condition line.contains(word)

If line contains that word , in then section
Assign that line to a string variable ‘Line’ and also array index to integer variable ‘index’ and then use break activitiy to exit out of loop

create integer variable step , intalise it with zero

Create another string variable a , assign it with “”

After that create another while loop with condition step <=index

inside the while loop , put if condition with condition as step <index:
if this condition satisfies in then section ,

Assign a = a+ Environment.NewLine
Assign step = step+1

In else condition put like this

Assign a = a+ Line

Out of loop use write text activitiy to.write the string ‘a’ to same notepad file in the same position earlier it was

Hope you got the logic

Mark it as solution if you got it

Nived N :robot:

Happy Automation :relaxed:


No need to delete ALL lines before and after. That’s the same as taking ONLY that line. Follow the beginning steps @NIVED_NAMBIAR mentioned: Split string on new line, for each str in ArrStr if str.toupper.contains(“MYWORD”) Then assign MyLine = str, break activity.

Now you have a string variable called MyLine that contains only the nth Line containing MyWord


Thanks a lot @NIVED_NAMBIAR , Possile to generate an example for the same?
I have started the process and fail to continue due to some points that still not clear for me.Main.xaml (9.8 KB) Thanks in advance

Thanks @ Dave, but what if we have the word repeated many times ? the aim of doing this is to keep only the 1st line where the word occurs for the 1st time and following procedure shared by @NIVED_NAMBIAR this can be achieved I guess, still only some steps not clear for me.

Because of the break activity, it will stop after it finds the first line containing the word

1 Like

Thanks @Dave, Could you please share the same in process?

Hi @hsendel. Will share the process :blush:

Nived N :robot:
Happy Automation

1 Like

Hi @hsendel , please check the workflow. (31.8 KB)

Mark it as solution if you got it

Nived N :robot::robot::robot::robot:

Happy Automation :blush:

1 Like

Thanks @NIVED_NAMBIAR, i will check and let you know :+1:

1 Like

Hello @NIVED_NAMBIAR, Seems there is a missing package as per the bellow error msg :

That is split string activitiy provided by workflow manager package , I had disabled it

U can use this to split the string

Already I had used the string manipulation to split the string

So delete that split string activitiy. I add that activitiy for testing ,

I will share the updated workflow now

Nived N :robot:
Happy Automation

@NIVED_NAMBIAR, It’s deleting everything including line where my word exists !!!

Hi @hsendel
please check the workflow
In this workflow, u can see sample text file in which there are many lines
i am searching misquote word i had assigned the word to input variable
and at last it is writing back to same text file (31.8 KB)

for ur clarification u can see that which word u want to search u want assign in input variable, in my case i had assigned with “misquote” u can see in image

Hope it helps

Mark it as solution if got it

Nived N :robot::robot:
Happy automation

1 Like

Unfortunately the result is an empty file even I delete the following error part from process :

sample.txt (1 Byte)

Hi @hsendel

Did you tried my updated workflow

Did u put the word u want to search in input variable

I was able to run it successfully

Please check my previous reply

Nived N :robot:

@NIVED_NAMBIAR, Of course I follow the last process you shared, and in attached file the video demo.Please Check it

How it happened so

In my case it was working fine

why so

view my entire workflow sequence


Nived N

Strange!!! I see you’re using newer version of Studio, Can this be an issue?

I got it, the activity I delete is “While” which is not present in my process, You asked me to delete it