Get the App
SLTechnology News&Howtos  ›  Development  › 

How to extract substrings from Bash

Shulou Source: shulou.com Published: 2022-06-01 13:58:59 10月24日 Update

This article is about how Bash extracts substrings. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The so-called "substring" is a string that appears in other strings. For example, "3382" is a substring of "this is a 3382 test". There are several ways to extract a number or a specified part of a string.

Extract substrings in Bash

Its syntax is:

# # format # # ${parameter:offset:length}

Substring extension is a feature of bash. It expands to a string in the parameter value that starts with offset and is length characters long. Suppose that $u is defined as follows:

# # define variable u # # u = "this is a test"

Then the substring extension of the following parameters extracts the substring:

Var= "${uvl 10 var 4}" echo "${var}"

The result is:

Test

These parameters represent:

10: offset position 4: length

Use IFS

According to the man page description of bash:

IFS (internal field delimiter) is used to split words after expansion and to split lines into words with the built-in read command. The default value is.

Another POSIX-ready POSIX ready scenario is as follows:

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

The output is:

Thisisatest

The following is a piece of bash code that removes the url with a home page from Cloudflare cache.

#! / 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-- $uecho "${1} ${IFS} ${IFS} ${3} ${IFS} ${4} ${IFS}"} echoecho "Purging cache from Cloudflare. ... " Echofor u in $urlsdohome_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}\"]} "echodoneecho

It can be used as follows:

~ / bin/cf.clear.cache https://www.cyberciti.biz/faq/bash-for-loop/https://www.cyberciti.biz/tips/linux-security.html with the help of cut command

You can use the cut command to delete each line or part of a variable in the file. Its syntax is:

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

To learn more, read bash's man page:

Man bashman cut

Thank you for reading! This is the end of the article on "how to extract substrings from Bash". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Characters strings commands more content parameters variables that is methods articles syntax extraction good practical one line home page from code location usage Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi Huawei Redmi Apple