HomeLinuxBash Substring after a Specified Character

Bash Substring after a Specified Character


The strategy of extracting the a part of a string is named the “substring”. No substring() technique exists in Bash to extract a substring from a string however the substring may be extracted from a string utilizing the Bash substring extraction and different instructions of Linux akin to “awk”, “lower”, “expr”, and so forth. The makes use of of Bash substring extraction to extract the substring after a specified character are defined on this tutorial.

Syntax:

The syntax of Bash substring extraction is as follows:

${variable:offset:size}

Right here, the variable accommodates a string worth. The offset accommodates the beginning place of the primary string from the place the substring is extracted and the worth of the offset may be any constructive or unfavourable integer. The size accommodates the entire size of the substring that’s extracted.

Totally different Examples of Substring Extraction

The other ways of extracting the substring after the desired place of the primary string are proven on this a part of the tutorial.

Instance 1: Extract the Substring from the Begin of the String

Create a Bash file with the next script that extracts a substring from a string of a number of phrases utilizing a constructive beginning place and size. The beginning place is about to 0 and the substring size is about to 4 within the script.

#!/bin/bash

#Outline a string variable

mainStr=“Bash is a well-liked programming language”

#Print the string variable

printf “The principle string worth:n$mainStr

#Create a substring by extracting from the primary character

#of the primary string and the size of the substring will probably be 4

subStr=${mainStr:0:4}

#Print the substring worth

printf nnThe substring worth:n$subStrn

The next output seems after executing the script. The substring worth is “Bash” if the substring is extracted from the 0 positions with the size of 4 from the string worth, “Bash is a well-liked programming language”.

Instance 2: Extract the Substring from the Center of the String

Create a Bash file with the next script that extracts a substring from a string of a number of phrases utilizing the constructive beginning place that’s larger than 0 and the constructive size worth. The beginning place is about to 10 and the substring size is about to 7 within the script.

#!/bin/bash

#Outline a string variable

mainStr=“Bash is a well-liked programming language”

#Print the string variable

printf “The principle string worth:n$mainStr

#Create a substring by extracting from the tenth place

#of the primary string and the size of the substring will probably be 7

subStr=${mainStr:10:7}

#Print the substring worth

printf nnThe substring worth:n$subStrn

The next output seems after executing the script. The substring worth is “well-liked” if the substring is extracted from the ten positions with the size of seven from the string worth, “Bash is a well-liked programming language”.

Instance 3: Extract the Substring with the Constructive Beginning Place Solely

Create a Bash file with the next script that extracts a substring from a string of a number of phrases utilizing a constructive beginning place solely. The beginning place is about to 18 within the script. If no size worth is about, the remaining a part of the string is extracted from the beginning place as a substring.

#!/bin/bash

#Outline a string variable

mainStr=“Bash is a well-liked programming language”

#Print the string variable

printf “The principle string worth:n$mainStr

#Create a substring by extracting from the after the

#18th place of the primary string

subStr=${mainStr:18}

#Print the substring worth

printf nnThe substring worth:n$subStrn

The next output seems after executing the script. The substring worth is “programming language” if the substring is extracted from the 18th place with no size worth from the string worth, “Bash is a well-liked programming language”.

Instance 4: Extract the Substring with the Unfavourable Beginning Place Solely

Create a Bash file with the next script that extracts a substring from a string of a number of phrases utilizing a unfavourable beginning place solely. The beginning place is about to -8 within the script. The beginning place is counted from the precise facet of the string and the counting begins from 1 if a unfavourable beginning place is used.

#!/bin/bash

#Outline a string variable

mainStr=“Bash is a well-liked programming language”

#Print the string variable

printf “The principle string worth:n$mainStr

#Create a substring by extracting the final 8 characters

#from the primary string

subStr=${mainStr:(-8)}

#Print the substring worth

printf nnThe substring worth:n$subStrn

The next output seems after executing the script. The substring worth is “language” if the substring is extracted from the -8 place from the string worth, “Bash is a well-liked programming language”.

 

Instance 5: Extract Substring with the Unfavourable Beginning Place and Constructive Size

 

Create a Bash file with the next script that extracts a substring from a string of a number of phrases utilizing a unfavourable beginning place and a constructive size. The beginning place is about to -20 and the size is about to 11 within the script.

 

#!/bin/bash

#Outline a string variable

mainStr=“Bash is a well-liked programming language”

#Print the string variable

printf “The principle string worth:n$mainStr

#Create a substring by extracting from the twentieth place and

#from the precise facet of the primary string and the size of the

#string is 11 characters

subStr=${mainStr:(-20):11}

#Print the substring worth

printf nnThe substring worth:n$subStrn

The next output seems after executing the script. The substring worth is “programming” if the substring is extracted from the -20 place with the size of 11 from the string worth, “Bash is a well-liked programming language”.

Conclusion

Varied methods of extracting the substring from a string information utilizing the Bash script are proven on this tutorial utilizing easy examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments