Skip to content
Advertisement

How to extract specific line in text file

I am text mining a large document. I want to extract a specific line.

JavaScript

I want to extract the description immediately under ITEM DESCRIPTION.

I have tried many unsuccessful attempts.

My latest attempt was:

JavaScript

But it did not find the text.

Is there a way to find ITEM DESCRIPTION and get the line after it or something similar?

Advertisement

Answer

The following function finds the description on the line below some given pattern, e.g. “ITEM DESCRIPTION”, and also ignores any blank lines that may be present in between. However, beware that the function does not handle the special case when the pattern exists, but the description does not.

JavaScript

I’ve assumed you got your text as a text string, and thus the function below will split it into a list of lines ..

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement