Skip to main content

Shell string functions for text handling. Join multiple strings, change casing, pad, searching, and more...

#!/usr/bin/sh

#===================================================================
s_join() #  creates single string from several
#===================================================================
# Arg_n = Input Strings
{
    echo "$@" | tr ' ' '_'
}

#===================================================================
s_upper() #  returns input string in upper case
#===================================================================
{
    echo $@ | tr '[a-z]' '[A-Z]'
}

#===================================================================
s_lower() #  returns input string in lower case
#===================================================================
{
    echo $@ | tr '[A-Z]' '[a-z]'
}

#===================================================================
s_count_args() #  returns count of input ARG_LIST
#===================================================================
# Arg_n = ARG_LIST
{
    echo $#
}

#===================================================================
s_count_lines() #  returns count of lines in FILENAME
#===================================================================
# Arg_1 = FILENAME
{
    line_count=`wc -l $1 | cut -c1-8`
    expr $line_count + 0
}

#===================================================================
s_length() #  returns length of string
#===================================================================
# Arg_1 = string
{
    length=`echo "$@" | wc -c | cut -c1-8`
    length=`expr $length -1`
}

#===================================================================
s_right_pad() #  right pads a string to width
#===================================================================
# Arg_1 = string
# Arg_2 = width
{
    string="$1                                         "
    echo "$string" | cut -c1-$2
}

#===================================================================
s_left_pad() #  left pads a string to width
#===================================================================
# Arg_1 = string
# Arg_2 = width
{
    string="                                         $1"
    length=`s_length "$string"`
    echo "$string" | cut -c`expr $length - $2`-$length
}

#===================================================================
s_find_char_all() #  finds all positions of CHAR in LIST
#===================================================================
# Arg_1 = CHAR
# Arg_n = LIST
{
    echo "$@" | cut -f2- -d\ | sed -e 's/./&\
    /g' | grep -n $1 | cut -f1 -d:
}

#===================================================================
s_find_char() #  finds first position of CHAR in LIST
#===================================================================
# Arg_1 = CHAR
# Arg_n = LIST
{
        s_find_word_all "$@" | head -1
}

#===================================================================
s_get_char() #  returns the CHAR at POSITION in LIST
#===================================================================
# Arg_1 = POSITION
# Arg_n = LIST
{
    echo "$@" | cut -f2- -d\ | cut -c$1 -d\
}

#===================================================================
s_find_word_all() #  finds all positions of word in list
#===================================================================
# Arg_1 = word
# Arg_n = list
{
    echo $@ | cut -f2- -d\ | tr ' ' '\n' \
        | grep -n $1 | cut -f1 -d:

#===================================================================
s_find_word() #  finds first position of word in list
#===================================================================
# Arg_1 = word
# Arg_n = list
{
    s_find_word_all $@ | head -1
}

#===================================================================
s_get_word() #  returns the word at position in list
#===================================================================
# Arg_1 = position
# Arg_n = list
{
    echo $@ | cut -f2- -d\ | cut -f$1 -d\
}