Read Specific Line From a Text File

Hello All,
I have two scenario

  1. How to Split the text file in for each loop from and before some specific text. I need to split from I have a car and till Truck (which is before I have a car in the last line).
    ex: I have a car
    Bike
    Truck
    I have a car
  2. I have a text file with multiple lines, i need to get specific data from each line by passing dynamic line number.
    ex: I have a car
    Bike
    Truck
    I have a car
    Thank you.

Hi @Ice, welcome to UiPath Community!

Can you please try the following -
ArrayVariable = YourStringVariable.Split(New String() {Environment.NewLine},StringSplitOptions.None)

Use assign activity for this.

Hi @Ice
Give a try to this method.

  1. Read text file using read text file activity.
    beforeString= Split(inputString,“I have a car”)(0).ToString
    afterString=Split(inputString,“I have a car”)(1).ToString


Sample Workflow
Split_Text.xaml (6.2 KB)

Hi @Vamsi_Krishna_Gogineni Thank you so much. This returns “System.Sting”. I have declared the variable as array. Also, Where do i need to provide the split variable which is “I have a car”

Hi @Ice,

For this, you can use YourStringVariable.Split(“I have a car”, StringSplitOptions.RemoveEmptyEntries) which gives you the string between ‘I have a car’ in line 1 - 4. Like below, as one string, and as a first item of the resultant string array.
image

For this, you can use YourStringVariable.Split(New String() {Environment.NewLine},StringSplitOptions.None) which returns each line as an individual string in string array.

StringSplit.xaml (11.8 KB)
New Text Document.txt (39 Bytes)

I’ve attached sample workflow for reference. Please run it and let me know if it helps.
Note: Please indicate the correct path of the txt file if you download it from here.

Hi @Suraj_B_Shetty , Thank you.
Below scenario could be infinite. “I have a car” is like a title, I need to split the data until next title and read the information under each title line by line. Lines may vary between one title to another. In the last we will not get the title, in this case i’m missing the last paragraph.

I have a car
Bike
Truck
I have a car
Bike
Truck
I have a car
Bike
Truck
I have a car
Bike
Truck

Hi @Vamsi_Krishna_Gogineni,
“I have a car” is like a title, I need to split the data until next title and read the information under each title line by line. Lines may vary between one title to another. In the last we will not get the title, in this case I’m missing the last paragraph.

I have a car
Bike
Truck
I have a car
Bike
Truck
I have a car
Bike
Truck
I have a car
Bike
Truck

Hello @Ice,

Below is the input I’ve taken and I have named the values 1, 2, 3 & 4 to represent them as individual sets/ paragraphs.
image

And below is the output I’m getting. First I’m getting the individual sets and then by splitting it further, I get to access each value of that set/ paragraph. It works dynamically with any number of line in-between ‘I have a car’ to ‘I have a car’.

StringSplit.xaml (12.7 KB)
Please find the updated XAML attached. Initially I thought there are 2 scenarios irrelevant to each other. Hope the latest one helps.

Hi @Vamsi_Krishna_Gogineni ,

I got the same xaml file as before. Please confirm if the below SS flow is correct

Also I got version error which i couldn’t add here.

Hi @Vamsi_Krishna_Gogineni ,

I’m using 2022.4.6. I’m not able to get you XAML properly due to version difference.
I have Attached the text file. I need to split the data from ABCDE123 before ABCDE123 of next item and then read specific value from each line item. I have the position of each data. Ex: Line 9: Position 11-13 for File name. I need to pass dynamic line value and check if there is a data.

Would you be able to share a screen shot of the flow, so that i can create my own flow and test it. Also the variable type. Thank you.

New Text Document.txt (1.0 KB)

Hi @Ice, I believe you’re referring to ‘AAA’ in below screenshot.

I’ve built the code accordingly and output is as below. Code is dynamic and fetching all the values
image

Below is the clear steps on what activities and code needs to be replicated on your end.

Code.docx (75.2 KB)
Once it is working, you can remove IndividualDataLines(8).Split(" ", StringSplitOptions.RemoveEmptyEntries)(0).ToString + " - " + from log message activity. I used it just to let you know about from which line the value is being extracted, by referencing to Name.

And variable below -

  • String[ ] is array of string type.
1 Like

Hi @Vamsi_Krishna_Gogineni ,

Thank you so much for your quick response. I’m getting the below error while assigning the first value.
I have assigned it as array of string.

Thanks

Hi @Ice, Can you please share the workflow you built!

Hi @Vamsi_Krishna_Gogineni ,

Main.xaml (7.1 KB)

I have not provided the Path here since it will be different. Thank you.

Hi @Ice,
Please update:

  1. Right side value of 1st assign to InputString.Split({“ABCDE123”},StringSplitOptions.RemoveEmptyEntries)
  2. Right side value of 2nd assign to
    currentItem.Split({Environment.NewLine}, StringSplitOptions.None)
  3. IndividualDataLines(8).Split(“{ }”, StringSplitOptions.RemoveEmptyEntries)(0).ToString

Just added { and }

1 Like

Hi @Vamsi_Krishna_Gogineni , Thank you so much. Its working but I still get the same error for Log message.

Please explain me what is the purpose of this log message , just for my understanding.

  1. IndividualDataLines(8).Split(“{ }”, StringSplitOptions.RemoveEmptyEntries)(0).ToString

Capture|690x304

Aah! my bad, Please update the { and } position as mentioned below.

IndividualDataLines is array of string variable which contains each separate line as an item in the array. 8 is the position of the line we need in that array post split. Again from that line, we are splitting the data using tab space and getting the 1st and 2nd values to get person name and file name.

Please update { and } and positions for both the values printing in log message.
IndividualDataLines(8).Split({" “}, StringSplitOptions.RemoveEmptyEntries)(1).ToString + " - " +IndividualDataLines(8).Split({” "}, StringSplitOptions.RemoveEmptyEntries)(1).ToString

Please make sure to have tab space between " ", not just single space.

1 Like

I have given tab under the brackets, but i’m still getting error, Am i missing anything.

My bad, I used the actual data in the code.

Is that a option to check the output array value in all the three steps. I couldn’t find what is the issue in my actual data.
If i can check the output, i will be able to know if its returning any value after the first step.

Yes, between assign and log message in the below screenshot, you can add one more log message and print currentItem
image

And after that you can use IndividualDataLines(8).Split(" ", StringSplitOptions.RemoveEmptyEntries) in another for each like below and print it’s currentItem
image
In my scenario, I update it to currentItem1 to keep it separate from above currentItem