Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 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
|
||||
13
Task/Tokenize-a-string/UNIX-Shell/tokenize-a-string-4.sh
Normal file
13
Task/Tokenize-a-string/UNIX-Shell/tokenize-a-string-4.sh
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
string1="Hello,How,Are,You,Today"
|
||||
elements_quantity=$(echo $string1|tr "," "\n"|wc -l)
|
||||
|
||||
present_element=1
|
||||
while [ $present_element -le $elements_quantity ];do
|
||||
echo $string1|cut -d "," -f $present_element|tr -d "\n"
|
||||
if [ $present_element -lt $elements_quantity ];then echo -n ".";fi
|
||||
present_element=$(expr $present_element + 1)
|
||||
done
|
||||
echo
|
||||
|
||||
# or to cheat
|
||||
echo "Hello,How,Are,You,Today"|tr "," "."
|
||||
Loading…
Add table
Add a link
Reference in a new issue