21-04-22 02:41 PM
21-04-22 03:17 PM
22-04-22 06:48 PM
23-04-22 01:59 AM
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:
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:
Add a code stage and map the input and output parameters as shown below:
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:
For the given input parameters:
Following results are generated: