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 VariablesDim previousText As StringDim subsequentText As String' Read Input Text FileDim fileContents = File.ReadAllLines(File_Path)' Get List Of Macthing Indices With Search WordDim matchingIndices= fileContents.Select(Function(x,i) i).Where(Function(i) fileContents(i).ToString.Contains(Search_Text))' Generate Output Collection SchemaLines.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 IndicesDim row As DataRowFor Each item In matchingIndicessubsequentText = ""previousText = ""row = Lines.NewRow' Add Matching Line Number Valuerow("Line Number") = item' Get Previous & Subsequent Text ValuesTryFor i = Previous_Text_Limit To Previous_Text_Threshold + Previous_Text_LimitIf previousText = "" ThenpreviousText = fileContents(item + 1 - i)ElsepreviousText = previousText + Environment.NewLine + fileContents(item + 1 - i)End IfNextFor i = Subsequent_Text_Limit To Subsequent_Text_Limit + Subsequent_Text_ThresholdIf subsequentText = "" ThensubsequentText = fileContents(item + i)ElsesubsequentText = subsequentText + Environment.NewLine + fileContents(item + i)End IfNextCatch ex As ExceptionEnd Try' Add Previous Text & Subsequent Text As Calculatedrow("Previous Text") = previousTextrow("Subsequent Text") = subsequentText' Add New Row To Output CollectionLines.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: