Blog

03/06/2019

How to extract substring in Bash



I have a bash shell variable called u="this is a test". How can I extract the “test” string and store into a shell variable?

A substring is nothing but a string is a string that occurs “in”. For example “3382” is a substring of “this is a 3382 test”. One can extract the digits or given string using various methods.

This quick tutorial shows how to obtain or finds substring when using bash shell.

Extract substring in Bash

The syntax is:
## syntax ##
${parameter:offset:length}

The substring expansion is a bash feature. It expands to up to length characters of the value of parameter starting at the character specified by offset. For example, $u defined as follows:

## define var named u ##
u="this is a test"

## define var named u ## u="this is a test"

The following substring parameter expansion performs substring extraction:

var="${u:10:4}"
echo "${var}"

var="${u:10:4}" echo "${var}"

Sample outputs:

test

Where numbers represents,

  • 10 : The offset
  • 4 : The length

Using IFS

From the bash man page:

The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>.

Another POSIX ready solution is as follows:

u="this is a test"
set -- $u
echo "$1"
echo "$2"
echo "$3"
echo "$4"

u="this is a test" set — $u echo "$1" echo "$2" echo "$3" echo "$4"

Sample outputs:

this
is
a
test

Here is a bash code that purge urls from Cloudflare cache along with home page:

#!/bin/bash
####################################################
## Author - SXI ADMIN {https://sxi.io/}
## Purpose - Purge CF cache
## License - Under GPL ver 3.x+
####################################################
## set me first ##
zone_id="YOUR_ZONE_ID_HERE"
api_key="YOUR_API_KEY_HERE"
email_id="YOUR_EMAIL_ID_HERE"
 
## hold data ##
home_url=""
amp_url=""
urls="$@"
 
## Show usage 
[ "$urls" == "" ] && { echo "Usage: $0 url1 url2 url3"; exit 1; }
 
## Get home page url as we have various sub dirs on domain
## /tips/
## /faq/
 
get_home_url(){
	local u="$1"
	IFS='/'
	set -- $u
	echo "${1}${IFS}${IFS}${3}${IFS}${4}${IFS}"
}
 
echo
echo "Purging cache from Cloudflare..."
echo
for u in $urls
do
     home_url="$(get_home_url $u)"
     amp_url="${u}amp/"
     curl -X DELETE "https://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache" 
          -H "X-Auth-Email: ${email_id}" 
          -H "X-Auth-Key: ${api_key}" 
          -H "Content-Type: application/json" 
          --data "{"files":["${u}","${amp_url}","${home_url}"]}"
     echo
done
echo

#!/bin/bash #################################################### ## Author – SXI ADMIN {https://sxi.io/} ## Purpose – Purge CF cache ## License – Under GPL ver 3.x+ #################################################### ## set me first ## zone_id="YOUR_ZONE_ID_HERE" api_key="YOUR_API_KEY_HERE" email_id="YOUR_EMAIL_ID_HERE" ## hold data ## home_url="" amp_url="" urls="$@" ## Show usage [ "$urls" == "" ] && { echo "Usage: $0 url1 url2 url3"; exit 1; } ## Get home page url as we have various sub dirs on domain ## /tips/ ## /faq/ get_home_url(){ local u="$1" IFS=’/’ set — $u echo "${1}${IFS}${IFS}${3}${IFS}${4}${IFS}" } echo echo "Purging cache from Cloudflare…" echo for u in $urls do home_url="$(get_home_url $u)" amp_url="${u}amp/" curl -X DELETE "https://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache" -H "X-Auth-Email: ${email_id}" -H "X-Auth-Key: ${api_key}" -H "Content-Type: application/json" –data "{"files":["${u}","${amp_url}","${home_url}"]}" echo done echo

I can run it as follows:
~/bin/cf.clear.cache https://sxi.io/bash-for-loop/ https://sxi.io/tips/linux-security.html

Say hello to cut command

One can remove sections from each line of file or variable using the cut command. The syntax is:

u="this is a test"
echo "$u" | cut -d' ' -f 4
echo "$u" | cut --delimiter=' ' --fields=4
##########################################
## WHERE
##   -d' ' : Use a whitespace as delimiter
##   -f 4  : Select only 4th field
##########################################
var="$(cut -d' ' -f 4 <<< $u)"
echo "${var}"

u="this is a test" echo "$u" | cut -d’ ‘ -f 4 echo "$u" | cut –delimiter=’ ‘ –fields=4 ########################################## ## WHERE ## -d’ ‘ : Use a whitespace as delimiter ## -f 4 : Select only 4th field ########################################## var="$(cut -d’ ‘ -f 4 <<< $u)" echo "${var}"

For more info read bash man page:
man bash
man cut

See also: Bash String Comparison: Find Out IF a Variable Contains a Substring

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....