Linux bash split string

Bash Split String

Bash Split String – Often when working with string literals or message streams, we come across a necessity to split a string into tokens using a delimiter. The delimiter could be a single character or a string with multiple characters. In this tutorial, we shall learn how to split a string in bash shell scripting with a delimiter of single and multiple character lengths.

Bash Split String

Split String with single character delimiter(s) in Bash using IFS

To split a string in bash using IFS, follow the below steps:

Step 1: Set IFS to the delimiter you would want. IFS=» IFS is an internal variable that determines how Bash recognizes word boundaries. The default value of IFS is white space. If you set it to some other value, reset it to default whitespace.

Step 2: Read your string to a variable with options -ra. read -ra ARR

Option Description
-r Backslash does not act as an escape character.
-a ARR The words(separated by IFS) are assigned to the sequential index of array ARR beginning at zero.

Now you have your string split by the delimiter (set in IFS) stored in array ARR. ARR is just array name. Any string literal that is valid could be used as an array name.

Step 3: You may now access the tokens split into an array using a bash for loop.

Its time for some examples. In the following two examples, we will go through example bash scripts where we use IFS to split a string.

Читайте также:  Запустить графическую оболочку kali linux

Example 1: Bash Split String by Space

In this example, we split a string using space as a character delimiter.

Bash Script File

#!/bin/bash str="Learn to Split a String in Bash Scripting" IFS=' ' # space is set as delimiter read -ra ADDR "; do # access each element of array echo "$i" done

Run the above bash shell script in a terminal.

$ ./bash-split-string-example Learn to Split a String in Bash Scripting

Example 2: Bash Split String by Symbol

Sometimes we may need to split a string by a delimiter other than space. To split a string in bash shell by a symbol or any other character, set the symbol or specific character to IFS and read the string to a variable with the options -ra mentioned in the below example.

Bash Script File

#!/bin/bash str="Learn-to-Split-a-String-in-Bash-Scripting" IFS='-' # hyphen (-) is set as delimiter read -ra ADDR "; do # access each element of array echo "$i" done IFS=' ' # reset to default value after usage

Run the above bash shell script in terminal.

$ ./bash-split-string-example-1 Learn to Split a String in Bash Scripting

The default value of IFS is single space ‘ ‘ . We changed the value of IFS to split a string. It should not affect any further script that is dependent on IFS. So, assign the default value back to IFS.

Split String with multiple character delimiter

To split a string with a multiple character delimiter (or simply said another string), following are two of the many possible ways, one with idiomatic and the other with just basic bash if and bash while loop.

Example 3: Split String with another string as delimiter idiomatic expressions

In this example, we will use idiomatic expressions where parameter expansion is done, and thus a very compact script.

Bash Script File

#!/bin/bash str="LearnABCtoABCSplitABCaABCString" delimiter=ABC s=$str$delimiter array=(); while [[ $s ]]; do array+=( "$" ); s=$; done; declare -p array

When you run the above script in a Bash Shell, you will see an output similar to the following

$ ./bash-split-string-example-3 declare -a array='([0]="Learn" [1]="to" [2]="Split" [3]="a" [4]="String")'

Following Parameter-Expansions are used (Reference: Bash Man Page[https://linux.die.net/man/1/bash])

Читайте также:  Linux mint обновить chromium
Expansion Description
$ Remove the longest matching suffix pattern.
$ Remove shortest matching prefix pattern.

Example 4: Bash Split a String with multiple character delimiter

If you are new to bash shell scripting and are not familiar with idiomatic expressions, following is an easily understandable shell script with basic bash if, bash while and bash substring methods. Comments are provided at each step to make the script file more readable.

Bash Script File

#!/bin/bash # main string str="LearnABCtoABCSplitABCaABCStringABCinABCBashABCScripting" # delimiter string delimiter="ABC" #length of main string strLen=$ #length of delimiter string dLen=$ #iterator for length of string i=0 #length tracker for ongoing substring wordLen=0 #starting position for ongoing substring strP=0 array=() while [ $i -lt $strLen ]; do if [ $delimiter == $ ]; then array+=($) strP=$(( i + dLen )) wordLen=0 i=$(( i + dLen )) fi i=$(( i + 1 )) wordLen=$(( wordLen + 1 )) done array+=($) declare -p array
$ ./bash-split-string-example declare -a array='([0]="Learn" [1]="to" [2]="Split" [3]="a" [4]="String" [5]="in" [6]="Bash" [7]="Scripting")'

The split strings are stored in the array and could be accessed using an index.

Conclusion

In this Bash Tutorial, we have learned how to split a string using bash script with different scenarios based on delimiter: like single character delimiter and multiple character delimiter.

Источник

How to Split String in Bash Script

In this quick tip, you’ll learn to split a string into an array in Bash script.

Let’s say you have a long string with several words separated by a comma or underscore. You want to split this string and extract the individual words.

You can split strings in bash using the Internal Field Separator (IFS) and read command or you can use the tr command. Let me show you how to do that with examples.

Method 1: Split string using read command in Bash

Here’s my sample script for splitting the string using read command:

#!/bin/bash # # Script to split a string based on the delimiter my_string="Ubuntu;Linux Mint;Debian;Arch;Fedora" IFS=';' read -ra my_array " do echo $i done

The part that split the string is here:

Let me explain it to you. IFS determines the delimiter on which you want to split the string. In my case, it’s a semi colon. It could be anything you want like space, tab, comma or even a letter.

The IFS in the read command splits the input at the delimiter. The read command reads the raw input (option -r) thus interprets the backslashes literally instead of treating them as escape character. The option -a with read command stores the word read into an array in bash.

In simpler words, the long string is split into several words separated by the delimiter and these words are stored in an array.

Now you can access the array to get any word you desire or use the for loop in bash to print all the words one by one as I have done in the above script.

Here’s the output of the above script:

Ubuntu Linux Mint Debian Arch Fedora

Method 2: Split string using tr command in Bash

This is the bash split string example using tr (translate) command:

#!/bin/bash # # Script to split a string based on the delimiter my_string="Ubuntu;Linux Mint;Debian;Arch;Fedora" my_array=($(echo $my_string | tr ";" "\n")) #Print the split string for i in "$" do echo $i done

This example is pretty much the same as the previous one. Instead of the read command, the tr command is used to split the string on the delimiter.

The problem with this approach is that the array element are divided on ‘space delimiter’. Because of that, elements like ‘Linux Mint’ will be treated as two words.

Here’s the output of the above script:

Ubuntu Linux Mint Debian Arch Fedora

That’s the reason why I prefer the first method to split string in bash.

I hope this quick bash tutorial helped you in splitting the string. In a related post, you may also want to read about string comparison in bash.

And if you are absolutely new to Bash, read our Bash beginner tutorial series.

Источник

Оцените статью
Adblock
detector