cancel
Showing results for 
Search instead for 
Did you mean: 

How to read desired lines from a text file ?

SwatiAgrawal
Level 5
Hi,

I have a text file. I want to perform following steps -
1. Find "agrawal" in the text file.
2. Wherever I have "agrawal", I want to read the line which is just above it.
3. Again, press "Find Next" for "agrawal" and read the line just above it. Keep doing this till search has finished.

Below is the sample text file.

26373.png
Please suggest.

Thanks in advance !!


------------------------------
Swati Agrawal
------------------------------
3 REPLIES 3

ewilson
Staff
Staff
Hi @Swati Agrawal,

There are a few ways you could do this. You don't mention is you're trying to do this via UI automation or not though. The first solution that comes to my mind is to use the Utility - File Management VBO​. There's an action called Read Lines from File. It will return a Collection of all the lines in the file. Then you could loop through the Collection looking for the lines that match your criteria. You would want to use a temporary data item to store each line as you go though so you have an easy way to recall the previous line when you match agrawal.

Does that make sense?

26363.png
If you were going to try and do this via UI automation, the same general principal would apply. As I mentioned there are multiple ways to do this. You could even drop into a Code stage in a VBO and use Linq or other options like OLEDB for pulling out the information.

Cheers,

------------------------------
Eric Wilson
Director, Integrations and Enablement
Blue Prism Digital Exchange
------------------------------

ManpreetKaur1
Level 8
Hi @Swati Agrawal,

As mentioned by @ewilson, the best way would be to use Utility File Management and applying the logic using calculation stages. Otherwise, you can use below options as well to perform the same task:

1) Exporting data in Excel and then utilizing 'Find Text' functionality to identify the cell reference. Post identification of cell reference, you can move the cell reference by one cell up and you can get the data.
2) Using code stage if you want to perform the operation really fast and making it a generic code.
3) Using LINQ or OLEDB​​

------------------------------
Manpreet Kaur
Manager
Deloitte
------------------------------

Hi Swati,

For your desired use case, I have created a code stage using LINQ and basic iterations that you can probably use. So first create a new business object and add the following 'Namespace Imports', 'External References' and 'Language' setting under the 'Code Options' tab of your page description stage in 'Initialise' action page:

26366.png
External References: System.Data.dll, System.Drawing.dll, System.Data.DataSetExtensions.dll, System.Core.dll,.System.Xml.dll
Namespace Imports: System, System.Data, System.Drawing, System.Data.DataSetExtensions, System.LINQ, System.Collections.Generic, System.IO
Language: Visual Basic

Now, you can create a new action page called as 'Get Previous and Subsequent Values' with the following input parameters:

- File Path (Text): The input file path of the text file where the operation needs to be performed.
- Search Text (Text): The keyword text to be used for search operation.
- Previous Text Threshold (Number): The starting index of the line relative to the line consisting of the keyword for which the previous text that needs to be grabbed in every continuation.
- Previous Text Limit (Number): The ending index of the line relative to the line consisting of the keyword for which the previous text that needs to be grabbed in every continuation.
- Subsequent Text Threshold (Number): The starting index of the line relative to the line consisting of the keyword for which the subsequent text that needs to be grabbed in every continuation.
- Subsequent Text Limit (Number): The ending index of the line relative to the line consisting of Subsequent Text Threshold for which the subsequent text that needs to be grabbed in every continuation.

Also, add an output parameter of type collection which should be an undefined collection. In my example, I have named it as 'Lines' as shown below:

26367.png

Add a code stage and map the input and output parameters as shown below:

26368.png

Add the following code:

' Initialize Local Variables

Dim previousText As String
Dim subsequentText As String

' Read Input Text File

Dim fileContents = File.ReadAllLines(File_Path)

' Get List Of Macthing Indices With Search Word

Dim matchingIndices= fileContents.Select(Function(x,i) i).Where(Function(i) fileContents(i).ToString.Contains(Search_Text))

' Generate Output Collection Schema

Lines.Columns.Add("Line Number", GetType(Integer))
Lines.Columns.Add("Previous Text", GetType(String))
Lines.Columns.Add("Subsequent Text", GetType(String))

' Iterate Through Each Item For Matching Indices

Dim row As DataRow

For Each item In matchingIndices

subsequentText = ""
previousText = ""

row = Lines.NewRow

' Add Matching Line Number Value

row("Line Number") = item


' Get Previous & Subsequent Text Values

Try

For i = Previous_Text_Limit To Previous_Text_Threshold + Previous_Text_Limit

If previousText = "" Then

previousText = fileContents(item + 1 - i)

Else

previousText = previousText + Environment.NewLine + fileContents(item + 1 - i)

End If

Next

For i = Subsequent_Text_Limit To Subsequent_Text_Limit + Subsequent_Text_Threshold

If subsequentText = "" Then

subsequentText = fileContents(item + i)

Else

subsequentText = subsequentText + Environment.NewLine + fileContents(item + i)

End If

Next

Catch ex As Exception


End Try

' Add Previous Text & Subsequent Text As Calculated

row("Previous Text") = previousText
row("Subsequent Text") = subsequentText


' Add New Row To Output Collection

Lines.Rows.Add(row)

Next


Now you can test the same with sample input file which in my case looks like this:

26369.png

For the given input parameters:

26370.png

Following results are generated:

26371.png



------------------------------
----------------------------------
Hope it helps you out and if my solution resolves your query, then please mark it as the 'Best Answer' so that the others members in the community having similar problem statement can track the answer easily in future

Regards,
Devneet Mohanty
Intelligent Process Automation Consultant | Sr. Consultant - Automation Developer,
Wonderbotz India Pvt. Ltd.
Blue Prism Community MVP | Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
Hope this helps you out and if so, please mark the current thread as the 'Answer', so others can refer to the same for reference in future.
Regards,
Devneet Mohanty,
SS&C Blueprism Community MVP 2024,
Automation Architect,
Wonderbotz India Pvt. Ltd.