Bash split string in text processing

137
linux

Sometimes, we need to split the string data into multiple segments for various purposes in programming. Almost languages today already have their own split built-in function for dividing data into multiple parts. But in bash shell, there is no split function for you to do this task. It means we have to have another way to split data into multiple segments without using split function. Normally, delimiters and white spaces are used to split any string data. How can we split data from a string into multiple parts? In this tutorial, I will show you some examples to do it in bash shell.

Bash split string examples with $IF variable

$IFS variable in bash shell is used to split a string data into multiple parts. $IF stands for Internal Field Separator (IFS) which is used to assign the specific delimiter to split a string data. By default, white space is delimiter value for $IFS variable to split string string. It means you also can use other characters like ‘\n‘, ‘\t‘, ‘‘, etc to be delimiter. After assigning a delimiter value, a string data which you want to split have two options to read are ‘-r‘ and ‘-a‘.

In the two options above, ‘-r‘ option is used to read bashslash character (\) rather than escape character and ‘-a‘ option is used to store the splited string into an array variable. The string data can be splited into multiple segments without $IFS variable in bash shell. And now I will show you some examples with $IFS variable and without $IFS variable.

Example 1: Split string data based on

By default, string data is divided by escape character, here I will create a file called example1.sh with the following code.

#!/bin/bash

#Define the string value
text="VSUDO is a great website"

# Set space as the delimiter
IFS=' '

#Read the split words into an array based on space delimiter
read -a strarr <<< "$text"

#Count the total words
echo "There are ${#strarr[*]} words in the text."

# Print each value of the array by using the loop
for val in "${strarr[@]}";
do
  printf "$val\n"
done

In the above code, you can see $text variable is assigned some words which is delimited by white space as a separator. And $IFS variable is assigned white space which is used to split string data. In read command, I use ‘-a‘ option to store splited data into an array variable called $strarr. Then for loop is used to print each element of the array $strarr.

When you excute the above script, you will get this output:

VSUDO
is
a
great
website

Example 2: Split string data based on custom character

In example 1, you have known that by default, $IFS will use white space as a delimiter to split string. Any specific character can be used as a seperator for dividing string data. Now, create the second file is call example2.sh with the following code:

#!/bin/bash

#Read the string value
echo "Enter vsudo to google, and then you will know me!"
read text

# Set comma as delimiter
IFS=','

#Read the split words into an array based on comma delimiter
read -a strarr <<< "$text"

#Print the splitted words
echo "Firt statement : ${strarr[0] }"
echo "Second statement : ${strarr[1]}"

In the above code, two statements are taken by adding ‘,’ character. And when I use option ‘-a’ I will store splited data into an array variable called $strarr. At the end of the code, I will print two statements and the output will be like:

Firt statement : Enter vsudo to google
Firt statement : and then you will know me!

Example 3: Split data string without $IFS variable

I have shown you two examples with $IFS, now I will show you another example without $IFS variable in bash shell. Create a file called example3.sh with the following code:

#!/bin/bash

#Read the main string
echo "Enter the string with colon(:) to split"
read mainstr

#Split the string based on the delimiter, ':'
readarray -d : -t strarr <<< "$mainstr"
printf "\n"

# Print each value of the array by using loop
for (( n=0; n < ${#strarr[*]}; n++))
do
  echo "${strarr[n]}"
done

According to the above script, script will say require enter an input with colon(:) character to split data. And -d ‘option’ is used to define the separator character which split string data without $IFS variable. Next, for loop is used to echo each elelement of the array.

When you run the about script you will get the output like this:

Enter the string with colon(:) to split
VSUDO:is:webdesigner

VSUDO
is
VSUDO:is:webdesigner

Conclusion

On processing string data, split data is needed by every programming languages. There are many ways of splitting string data into segments in bash shell are shown in this article. Hope over this tutorial and the above examples, you will be able to split string data in bash easily!

Previous articleViddyoze Review: Get Ready to Turn Videos Into Your Marketing Weapon
Next articleBash concatenate strings in text processing