Search text in file and return line

781 views
Skip to first unread message

Romulo Genova

unread,
Jul 25, 2015, 12:50:50 AM7/25/15
to Tasker
Hi everyone!

I'm having some headaches with this and I hope you can help me!

I need to make tasker search for one specific text string or multiple strings, like tags (any of these solutions would be great) and then, tasker teels me which line in this file contains this text...

I have a txt file with many lines, for example:

tag1,tag2,tag3 - lorem ipsum
tag4,tag5,tag6 - dolor sit amet
tag7,tag8,tag9 - consectetur adipiscing elit
...

If my profile asks for "tag4,tag5,tag6", tasker should return "2" and then I'll use READ LINE to get the full line to split and process the text to get "dolor sit amet"

Thanks in advance!

rayson

unread,
Jul 25, 2015, 5:05:07 AM7/25/15
to Tasker
Read the entire text into a variable (%text), split that variable using a line break as the splitter, then search the array you just generated (%text1, %text2,...) for the match (tag4,tag5,tag6), read the value of that matching variable (%text2) to another variable (%line) then split that variable using " - " as the splitter to get the value your looking for (%line2). You need to read the matching variable (%text2) to another variable(%line) because splitting %text2 might overwrite existing variables in the array. Also this should be easily modified to find multiple lines that match a certain tag. Also, in case you don't already know, you can't directly use a line break as a splitter (you can't enter a line break in the splitter field). You need to set a variable (%break) to a line break then use that variable as the splitter.

rayson

unread,
Jul 25, 2015, 5:24:57 AM7/25/15
to Tasker
Actually, there's a easier way to do what you want using the variable search replace function and regex. Read the entire text into a variable (%text), search for "tag4,tag5,tag6.+" and store matches in a variable (%match) then do another search and replace on %match to remove "tag4,tag5,tag6 - " which should leave you with the correct result ("dolor sit amet"). This should also be easily modifiable to find multiple matches.

Rich D

unread,
Jul 25, 2015, 7:52:50 AM7/25/15
to Tasker Google Groups Post


> If my profile asks for "tag4,tag5,tag6", tasker should return "2" and then I'll use READ LINE to get the full line to split and process the text to get "dolor sit amet"

-  Read file  To: %arr  (entire file NOT just one line)
-  Variable set %ret TO: (a actual return, IE press the return on the keyboard)
-  variable split %arr / Splitter: %ret
- Variable set %index to %arr(#?tag3)

Check the userguide / variables...

From the userguide;
%arr(#?b/c)
A comma-separated list of the array indices (lowest to highest) with matching values, or 0 if none match (2,3 in the example)

Rich D

unread,
Jul 25, 2015, 8:19:02 AM7/25/15
to Tasker Google Groups Post

One correction on this. You need to Make you search query like this *tag3*/*tag4*/*tag5*

> -  Read file  To: %arr  (entire file NOT just one line)
> -  Variable set %ret TO: (a actual return, IE press the return on the keyboard)
> -  variable split %arr / Splitter: %ret

> - Variable set %index to %arr(#?*tag3*/*tag4*/*tag5*)
>

I had misunderstood what the final goal was. I thought you just wanted the line #.  To get the data attached to the tag then I would recommend  Rayson's Variable search/replace method..

Romulo Genova

unread,
Jul 25, 2015, 12:08:40 PM7/25/15
to Tasker
I'll test it! Thank you a lot!

Romulo Genova

unread,
Jul 25, 2015, 12:09:12 PM7/25/15
to Tasker
Thanks for your help! I'll test this ;)
Reply all
Reply to author
Forward
0 new messages