This article will introduce how to extract substring from string.
1. Using offset
You can split a string within a certain index range with the following syntax. Offset
is the index
where the string starts, and length
is the length you want to cut from the offset.
echo "${STR:offset:length}"
In this example, it split the string from index 0 to 5 characters and stores them in the str
variable.
#!/bin/bash
str="Hello, World, Bash!"
echo "${str:0:5}"
Output:
$ bash example.sh
Hello
1.1 Example1
If no length is entered, the string will be cut off from the offset to the end of the string.
#!/bin/bash
str="Hello, World, Bash!"
echo "${str:7}"
Output:
$ bash example.sh
World, Bash!
1.2 Example2
If the offset is set to a negative number, the offset is calculated from the end of the string.
In this example, the offset of -5
would be the position of B
. And since no length was passed, it will be cut off until the end of the string.
#!/bin/bash
str="Hello, World, Bash!"
echo "${str:(-5)}"
Output:
$ bash example.sh
Bash!
If you pass the length, it will split the string from the offset for the length you input.
#!/bin/bash
str="Hello, World, Bash!"
echo "${str:(-5):2}"
Output:
$ bash example.sh
Bash!
2. Using cut
command
2.1 Example1
The cut -c 8
command takes only the 8th character from a string.
#!/bin/bash
str="Hello, World, Bash!"
echo $str | cut -c 8
Output:
$ bash example.sh
W
If you use like cut -c 8,12
, characters at 8th and 12th will be took.
#!/bin/bash
str="Hello, World, Bash!"
echo $str | cut -c 8,12
Output:
$ bash example.sh
Wd
2.2 Example2
You can specify the string range you want to take, like cut -c Start-End
. Note that the Offset of the string starts from 1.
In the following example, cut -c 8-12
takes the string from 8th to 12th character.
#!/bin/bash
str="Hello, World, Bash!"
echo $str | cut -c 8-12
Output:
$ bash example.sh
World
2.3 Example3
If you do not specify the end range of the string, it will take from the Start Index to the end of the string.
#!/bin/bash
str="Hello, World, Bash!"
echo $str | cut -c 8-
Output:
$ bash example.sh
World, Bash!
2.4 Example4
If you do not specify a start range for the string, it will take from the beginning to the end Index.
#!/bin/bash
str="Hello, World, Bash!"
echo $str | cut -c -5
Output:
$ bash example.sh
Hello
2.5 Example 5
When using the cut command, you can specify a delimiter and split the string based on that delimiter.
In this example, cut -d ':' -f1
will split the string by :
and store the first string in str
. If you want to get the second string, you can specify -f2
.
#!/bin/bash
str="Hello:World:Bash"
echo $str | cut -d ':' -f1
echo $str | cut -d ':' -f2
echo $str | cut -d ':' -f3
Output:
$ bash example.sh
Hello
World
Bash
3. Using pattern
You can find patterns in a string and delete those characters. In this way, You can delete unnecessary strings and get only the characters you need.
3.1 Example1
${STR#PATTERN}
removes the shortest string matching the pattern from the STR variable string.
In the example string below, the pattern A*B
would match AAAB
as the shortest string and AAABBB
as the longest string. The result of the execution would be the shortest string matching the pattern being removed.
#!/bin/bash
str="AAABBBCCC"
echo ${str#A*B}
Output:
$ bash example.sh
BBCCC
3.2 Example2
${STR##PATTERN}
searches for the longest string in STR variable that matches the pattern and deletes it.
For example, if the pattern A*B
is found in the string AAABBB
, the longest matching pattern AAABBB
will be deleted and the result will be AAAB
.
#!/bin/bash
str="AAABBBCCC"
echo ${str##A*B}
Output:
$ bash example.sh
CCC
3.3 Example3
${STR%PATTERN}
searches for a string in the STR variable that matches the pattern and includes the last string. It then deletes the pattern that matches the shortest.
For example, if you search for the pattern B*C
in the string below, BCCC
is the shortest pattern that matches and BBBCCC
is the longest. The result of the execution will be the deletion of the shortest matching string.
#!/bin/bash
str="AAABBBCCC"
echo ${str%B*C}
Output:
$ bash example.sh
AAABB
3.4 Example4
${STR%%PATTERN}
finds the longest string that matches the pattern in the STR variable, and removes it.
In the example string below, if we look for the pattern B*C
, the shortest matching pattern is BCCC
and the longest matching pattern is BBBCCC
. If you look at the result, the longest matching string is deleted.
#!/bin/bash
str="AAABBBCCC"
echo ${str%%B*C}
Output:
$ bash example.sh
AAA
Related Posts
- How to measure elapsed time in Bash
- Ceil, Floor, Round off a devided number in Bash
- Check if a number is Positive or Negative in Bash
- How to Print a Variable in Bash
- Number Comparison Operators in Bash
- Convert between Uppercase and Lowercase in Bash
- Check if a File or Directory exists in Bash
- How to extract substring in Bash
- Bash Case Statement with examples
- How to use Command Line Arguments in Bash
- How to use Arrays in Bash
- How to Compare strings in Bash Shell
- if..else statement in Bash
- Add, Subtract, Multiply, Division with Numbers in Bash
- Bash For, Whlie Loop Examples
- How to Pass and Use command line arguments in Bash