Example-1: Trim string data using parameter expansion
Space or any character can be trimmed easily from the string data by using bash parameter expansion. The following commands show the uses of parameter expansion for removing space from the starting and end of the string.
$ myVar=" everyone "
# The following command will show the spaces at the starting and end of the
variable, $myVar
$ echo "Hello $myVar"
# The following command will print the output after removing the spaces from
the beginning
of the variable, $myVar
$ echo "Hello ${myVar##*( )}"
#The following command will print the output after removing the spaces from
the ending of the
variable, $myVar
$ echo "${myVar%%*( )} is welcome to our site"
Example-2: Trim string data using `sed` command
`sed` command is another option to remove leading and trailing space or character from the string data. The following commands will remove the spaces from the variable, $myVar using `sed` command.
$ myVar=" Web Design Courses "
# The following command will print the output with the leading and trailing spaces of the
variable,$myVar
$ echo "I want to learn $myVar from this site"
# The following `sed` command will remove the trailing spaces from the variable
$ myVar=`echo $myVar | sed ‘s/ *$//g’`
# Print the output after removing the spaces
$ echo "I want to learn $myVar from this site"
Use sed ‘s/^ *//g’, to remove the leading white spaces.
There is another way to remove whitespaces using `sed` command. The following commands removed the spaces from the variable, $Var by using `sed` command and [[:space:]].
$ Var=" PHP and MySQL "
# Print the value of $Var before trimming
$ echo "$Var are very popular now."
#Remove the spaces from the variable
$ Var=`echo $Var | sed -e ‘s/^[[:space:]]*//’`
# Print the value of $Var after trimming
$ echo "$Var are very popular now."
Example-3: Trim string data using `awk` command
`awk` command is another way to trim the string value. The following commands uses `awk` command to remove spaces from the starting and ending of the variable, $Input_text.
$ Input_text=" Desiginning Website with CSS3 "
# Print the value of the variable before trimming
$ echo "${Input_text}"
# Print the string after Removing the spaces from the beginning of the variable
$ echo "${Input_text}" | awk ‘{gsub(/^[ t]+/,""); print $0, " JQuery" }’
# Print the string after Removing the spaces from the end of the variable
$ echo "${Input_text}" | awk ‘{gsub(/[ t]+$/,""); print $0, " JQuery" }’
# Print the string after Removing the spaces from the beginning and end of the variable
$ echo "${Input_text}" | awk ‘{gsub(/^[ t]+| [ t]+$/,""); print $0, " JQuery" }’
Example-4: Trim string data using xargs command
`xargs` is another simple command to trim string data.
$ echo " Bash Scripting Language " | xargs
Conclusion:
This tutorial shows the different ways to trim string data. The string data needs to trim for various reasons. For example, it is better to remove extra spaces from the starting and end of the data before inserting into the database or compare with other value. This tutorial will help the new users to learn trimming options in bash.