In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.