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"
The following substring parameter expansion performs substring extraction:
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"
Sample outputs:
this is a test
Here is a bash code that purge urls from Cloudflare cache along with home page:
#!/bin/bash #################################################### ## Author - Vivek Gite {https://www.cyberciti.biz/} ## 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://www.cyberciti.biz/faq/bash-for-loop/ https://www.cyberciti.biz/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}"
For more info read bash man page:
man bash
man cut
See also: Bash String Comparison: Find Out IF a Variable Contains a Substring
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 0 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |