LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

capture certain lines by certain keyword inside *.txt

Hi,
 
Recently I'm doing a system that need to browse a txt file. I managed to browse and open even read all texts inside the .txt file but i have no idea how to capture single or multiple lines by using certain keyword.
 
For an example, i have a *.txt which contain 20 lines of diffrents data, and within this 20 lines, line number 2 to line number 5 contain a word call "apple". So, how can i capture line number 2 to 5 by using the keyword call "apple"?
 
Anyone can answer? there is a million thanks!
 
Regards
Jaxx

Message Edited by @ on 09-27-2007 01:04 PM

0 Kudos
Message 1 of 5
(2,902 Views)
"Match Pattern" is your friend (though you can use similar functions).

If you're reading the file one line at a time, you just need to use the "Match Pattern" function to see if the word is in the line. If so, add it to whatever it is you're building, like an array of strings (which are the lines in the file) via a shift register. If you read the whole file into an array of strings, with each element being a line, you can just autoindex a for-loop and do the same thing.

I'll leave the details of implementation to you, as it's a good learning exercise, and not difficult. Smiley Wink
0 Kudos
Message 2 of 5
(2,889 Views)
What version of LabVIEW do you use?
 
You could read your entire file as a 1D array of strings using "spreadsheet string to array" with \n as delimiter. Now you can just autoindex into a FOR loop, and find the lines containg apple using "match pattern". (If you just use match pattern in the raw linear string, you'll have to figure the line numbers later with some extra code).
 
Reading a file line-by-line is typically slow, so that function is only useful to e.g. read the first few lines of a small file. (I am not sure if things have improved in recent versions).
0 Kudos
Message 3 of 5
(2,885 Views)

Hi,

Thanks for replied. But I not really understand what you mean Smiley Indifferent Anyway i need to read a .txt which contain few hundred lines of data. So the suggestion is still suitable? and i did tried out what you mean, but i stuck in the middle. the attachment is my work. Can you please take a look? and tell me the problem? Thanks!

Regards

Jaxx

0 Kudos
Message 4 of 5
(2,866 Views)
Try this:
 
(right now it finds lines with lowercase "apple" only. You would need to tweak it if you want a case insensitive match. Check the online help.)
 
0 Kudos
Message 5 of 5
(2,862 Views)