Tokenize a string: Difference between revisions

Content added Content deleted
(Fix UnixPipes for inputs like "many spaces,in string". Fit bash example in 80 columns. Two examples work with Bourne Shell, and bash example works also with pdksh.)
Line 1,360:
 
=={{header|UnixPipes}}==
{{works with|Bourne Shell}}
<lang bash>token() {
(IFS=, read -r A B; echo "$A".; test -n "$B" && (echo "$B" | token) )
}
 
Line 1,367 ⟶ 1,368:
 
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
<lang bash>string='Hello,How,Are,You,Today'
 
Line 1,373 ⟶ 1,375:
echo)</lang>
 
----
 
{{works with|Bourne Again SHell}}
=={{header|UNIX Shell}}==
{{works with|BournePublic AgainDomain ShellKorn SHell|5.2.14}}
<lang bash>#! /bin/bash
stripchar-l ()
Line 1,415 ⟶ 1,417:
local output_field_seperator=" ";
join "$list" "$input_field_seperator" "$output_field_seperator" #defined in terms of join
join "$list" "$input_field_seperator" "$output_field_seperator"
}
 
strtokenize ()
{
#splits up a string of characters into tokens, based on a user supplied delimiter
#based on a user supplied delimiter
#USAGE:strtokenize "1;2;3;4" ";" ":" --> 1:2:3:4
local list="$1";
local input_delimiter=${2-" "};
local output_delimiter=${3-" "};
local contains_a_space=" "; #added to highlight the use of " " as an argument to join
#of " " as an argument to join
join "$( split "$list" "$input_delimiter" )" "$contains_a_space" "$output_delimiter"; #splits it input then joins it with a user supplied delimiter
join "$( split "$list" "$input_delimiter" )" \
} </lang>
"$contains_a_space" "$output_delimiter";
} </lang>
 
''Example''