Split Variable into individual characters

849 views
Skip to first unread message

JS

unread,
Mar 10, 2011, 5:48:16 AM3/10/11
to Tasker
Is there a way to split a variable into individual characters, without
there being a splitter character?

E.g.

%LETTERS = abcd
==>
%LETTERS1 = a
%LETTERS2 = b
%LETTERS3 = c
%LETTERS4 = d

If not could I suggest it.

Many Thanks,
JS

Pent

unread,
Mar 10, 2011, 2:20:28 PM3/10/11
to Tasker
> Is there a way to split a variable into individual characters, without
> there being a splitter character?

Sorry, not that I know of.

Pent

fubaya

unread,
Mar 10, 2011, 7:10:45 PM3/10/11
to Tasker
As a workaround, have Tasker save the text to a file, I'll use
"yourfile" as an example, then use this command with the Local Execute
Plugin:

@ sed -i 's/./& /g' /sdcard/yourfile

That will insert a space between every character in the file, then
read the file and split the variables.

So the task would be something like:

1 write file yourfile %LETTERS
2 execute plugin: @ sed -i 's/./& /g' /sdcard/yourfile
3 read line (or paragraph) yourfile to %LETTERS
4 variable split %LETTERS using a space as the splitter.
...

You might need to add a wait between 2 and 3 but probably only if the
file is huge.

JS

unread,
Mar 17, 2011, 3:44:22 PM3/17/11
to tas...@googlegroups.com
Thanks for the solution fubaya, but struggling with the sed command I'm affraid!
 
I've installed the Locale Execute Plug-in
Using it in the Plugin > Execute
I've got it working with a different example:
@ mkdir /sdcard/js1
to create a "js1" subdirectory.
 
Having problems with the sed command:
Just to get it to work I'm trying:
@ sed -i 's/a/z/g' /sdcard/text.txt
 
My understanding so far:
-i overwrites the original file
s/ substitutes
a replace any 'a' character
z replace with 'z' character
g removes white space
with the file to use at the end.
 
I haven't got root permits (hoping I don't need as the mkdir example worked)
The file is showing as -rw (and sd card is writable too)
 
Please help! Thanks in advance.
 
 

fubaya

unread,
Mar 17, 2011, 6:22:48 PM3/17/11
to Tasker
You are close except for the g. The g goes with the s and together
mean search and replace. You can actually do it without the g, it's
just a habit of mine to use it.

My example tells sed to search and replace every character (.) with
the character and a space (& ). The "&" means the character and the
space is an actual space (I'm typing on my phone or else I'd look up a
more official definition).

@ sed -i 's/./& /g' /sdcard/yourfile
before = abc
after= a b c

If you want to remove spaces, use 's/ //g'

I'll have to check back later, I've got kids screaming in my ear. What
exactly do you want to do? My example should work for adding spaces.

fubaya

unread,
Mar 17, 2011, 7:32:34 PM3/17/11
to Tasker
On Mar 17, 6:22 pm, fubaya <jdm...@gmail.com> wrote:
> You are close except for the g. The g goes with the s and together
> mean search and replace. You can actually do it without the g, it's
> just a habit of mine to use it.
>

Oops, don't know what I was thinking, but the g is necessary. Without
it, it only replaces the first instance on a line, with it, it
replaces all instances. Just wanted to correct my mistake before I led
you astray.

BossMan

unread,
Mar 17, 2011, 7:36:04 PM3/17/11
to Tasker
Small clarification about /g - as far as I remember, it means
"globally", i.e. match as many times in a line as possible. If you do
not specify /g, a replacement will occur only once for each matching
line (the first match only). You can specify a number instead of /g
and sed will replace characters up to this number for occurencess
every line.

BR,
A.

BossMan

unread,
Mar 17, 2011, 7:36:55 PM3/17/11
to Tasker
Exactly :-)

BR,
A.

JS

unread,
Mar 17, 2011, 8:09:50 PM3/17/11
to Tasker
Ahh I see, that explaination of sed makes sense to me now. However
still having a problem.

Here is the xml for the 'LetterSplit' task:
http://pug.awardspace.co.uk/varsplit.xml

I'm doing: "@ sed -i 's/./& /g' /sdcard/test1.txt"
as you said but it the popup of the %TESTOUT is the same as %TESTOUT1
In a way I hope I'm doing something stupid! My phone is getting a bit
old school now, Its a HTC Hero running 2.1-update1. I hope that isn't
the problem

Thanks Again, (and hope the kids have settled a bit fubaya! :-) )
> > you astray.- Hide quoted text -
>
> - Show quoted text -

fubaya

unread,
Mar 17, 2011, 11:59:44 PM3/17/11
to Tasker
I can't import your task for some reason. It says "done" but doesn't
show up anywhere. I tried importing an existing task of my own and it
says "a task with that name already exists" but every time I try yours
it says "Done." so apparently yours isn't really importing. Maybe
because I'm on 2.3? The version shouldn't matter to sed though, it's a
very basic linux command that probably hasn't changed in years.

Anyway, if you can import mine, try this:
http://www.gogofile.com/Default.aspx?p=sc&ID=634360007641578750_2758

or here it is manually:

1- variable set %ONE to 1 (used to read only line 1 from the file,
otherwise it gets an EOF error every other time you run it. That's
just the way tasker is when reading files sometimes)

2- variable set %LETTERS to abc

3- write file test.txt %LETTERS

4- execute @ sed -i 's/./& /g' /sdcard/test.txt

5- read line file:test.txt line:%ONE to var %LINE

6- flash %LINE (it should flash "a b c")

7- variable split %LINE using a space as the splitter

8 - flash:
line1: %LINE1
line2: %LINE2
line3: %LINE3

If the split works, step 8 should flash:
line1: a
line2: b
line3: c

Step 7 may be giving a problem. When I add a space then go back to
edit that step, the space is gone. The task actually still works for
me though. Maybe it isn't meant to use spaces or maybe it will use a
space as default if you add nothing, but what I originally did was add
a space as the splitter, clicked done, and it worked. You could always
change the sed command to use something other than spaces, like 's/./
&Z/g' will make "abc" change to "aZbZcZ" then you could use Z as the
splitter.

Hope some of this helps.

(And luckily only one of the kids was mine, but she never settles!)

On Mar 17, 7:36 pm, BossMan <adam.marynow...@gmail.com> wrote:
> Small clarification about /g - as far as I remember, it means
> "globally",

D'oh. I thought and thought and couldn't come up with anything the g
could possibly stand for. I blame it on PERL users who often use s///
as the default example although I bet they, like me, never use it
without the g.

JS

unread,
Mar 20, 2011, 10:00:12 AM3/20/11
to Tasker
Couldn't import yours either! test.tsk.xml seems to be empty when I
open it.
Done as you've said (and hopfully exported it properly this time)

http://pug.awardspace.co.uk/SplitLetters.prf.xml
(added in %TIMES to the variable and the 2 seconds waits just to be
sure).

But the outcome is still without any spaces! If it works for you but
not me, I think I'll give up!

Thanks for your help. Cheers
John


On Mar 18, 3:59 am, fubaya <jdm...@gmail.com> wrote:
> I can't import your task for some reason. It says "done" but doesn't
> show up anywhere. I tried importing an existing task of my own and it
> says "a task with that name already exists" but every time I try yours
> it says "Done." so apparently yours isn't really importing. Maybe
> because I'm on 2.3? The version shouldn't matter to sed though, it's a
> very basic linux command that probably hasn't changed in years.
>
> Anyway, if you can import mine, try this:http://www.gogofile.com/Default.aspx?p=sc&ID=634360007641578750_2758
>
> or here it is manually:
>
> 1- variable set %ONE to 1 (used to read only line 1 from the file,
> otherwise it gets an EOF error every other time you run it. That's
> just the way tasker is when reading files sometimes)
>
> 2- variable set %LETTERS to abc
>
> 3- write file test.txt %LETTERS
>
> 4- execute @ sed -i 's/./& /g' /sdcard/test.txt
>
> 5- read line file:test.txt line:%ONE to var %LINE
>
> 6- flash %LINE (it should flash "a b c")
>
> 7- variablesplit%LINE using a space as the splitter
>
> 8 - flash:
> line1: %LINE1
> line2: %LINE2
> line3: %LINE3
>
> If thesplitworks, step 8 should flash:

fubaya

unread,
Mar 20, 2011, 10:14:41 PM3/20/11
to Tasker
Ahhhh... my fault. sed is part of busybox and you do need root to get
busybox. I could have sworn it came on the phone, it does on the
iphone. I would cuss up a storm right now but that wouldn't help..

I wasted your time but I found a workaround that uses the pure built-
in Android shell that should work and you only need to edit a couple
steps in your profile. But first, save this text into a file on the
sdcard, I'll use /sdcard/split in these examples but you can put it
anywhere:

#! /system/bin/sh
length=$(($#1 + 1))
count=1
while [ "$count" -lt "$length" ]; do
echo -n "$(expr substr $1 $count 1 ) "
count=$(($count + 1))
done
echo

Then in Tasker, edit these steps:

Run this in the execute plugin: @ sh /sdcard/split $(cat /sdcard/
test1.txt) > /sdcard/test1out.txt

Then read the line from /sdcard/test1out.txt, the contents should be
"a b c d e f "

I could make it re-use the same file, but this is simpler for now. You
can get rid of the wait before the plugin, but keep the one after it
for now and try to remove it later and see if it works. This little
script will probably be slower than sed, but shouldn't matter if you
only have a few letters.

Nicky Doyle

unread,
Sep 14, 2013, 11:24:15 PM9/14/13
to tas...@googlegroups.com
what is step 4 ie   Run this in the execute plugin:  could i just run a shell command ? im trying this out now cant get any good i have root btw

Matt R

unread,
Sep 14, 2013, 11:39:00 PM9/14/13
to tas...@googlegroups.com
You're digging up a way old thread that's out of date. It's much easier now using Variable Search Replace with . (dot) in the search field.

Matt

Nicky Doyle

unread,
Sep 15, 2013, 12:31:57 AM9/15/13
to tas...@googlegroups.com
i got it kinda working as in. if i run the task it spells the word but when i say the word using autovoice it doesnt break the word
Reply all
Reply to author
Forward
0 new messages