all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
5
Task/Tokenize-a-string/UNIX-Shell/tokenize-a-string-1.sh
Normal file
5
Task/Tokenize-a-string/UNIX-Shell/tokenize-a-string-1.sh
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
string='Hello,How,Are,You,Today'
|
||||
|
||||
(IFS=,
|
||||
printf '%s.' $string
|
||||
echo)
|
||||
58
Task/Tokenize-a-string/UNIX-Shell/tokenize-a-string-2.sh
Normal file
58
Task/Tokenize-a-string/UNIX-Shell/tokenize-a-string-2.sh
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#! /bin/bash
|
||||
stripchar-l ()
|
||||
#removes the specified character from the left side of the string
|
||||
#USAGE: stripchar "stuff" "s" --> tuff
|
||||
{
|
||||
string="$1";
|
||||
string=${string#"$2"};
|
||||
|
||||
echo "$string"
|
||||
}
|
||||
|
||||
join ()
|
||||
#join a string of characters on a specified delimiter
|
||||
#USAGE: join "1;2;3;4" ";" "," --> 1,2,3,4
|
||||
{
|
||||
local result="";
|
||||
local list="$1";
|
||||
OLDIFS="$IFS";
|
||||
local IFS=${2-" "};
|
||||
local output_field_seperator=${3-" "};
|
||||
|
||||
for element in $list;
|
||||
do
|
||||
result="$result$output_field_seperator$element";
|
||||
done;
|
||||
|
||||
result="`stripchar-l "$result" "$output_field_seperator"`";
|
||||
echo "$result";
|
||||
IFS="$OLDIFS"
|
||||
}
|
||||
|
||||
split ()
|
||||
{
|
||||
#split a string of characters on a specified delimiter
|
||||
#USAGE: split "1;2;3;4" ";" --> 1 2 3 4
|
||||
local list="$1";
|
||||
local input_field_seperator=${2-" "};
|
||||
local 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
|
||||
#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
|
||||
|
||||
#splits it input then joins it with a user supplied delimiter
|
||||
join "$( split "$list" "$input_delimiter" )" \
|
||||
"$contains_a_space" "$output_delimiter";
|
||||
}
|
||||
2
Task/Tokenize-a-string/UNIX-Shell/tokenize-a-string-3.sh
Normal file
2
Task/Tokenize-a-string/UNIX-Shell/tokenize-a-string-3.sh
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
strtokenize "Hello,How,Are,You,Today" "," "."
|
||||
Hello.How.Are.You.Today
|
||||
Loading…
Add table
Add a link
Reference in a new issue