自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Bash中抽取子字符串

系統(tǒng) Linux 系統(tǒng)運(yùn)維
所謂“子字符串”就是出現(xiàn)在其它字符串內(nèi)的字符串。 比如 “3382” 就是 “this is a 3382 test” 的子字符串。 本文會(huì)向你展示在 bash shell 中如何獲取或者說(shuō)查找出子字符串。

所謂“子字符串”就是出現(xiàn)在其它字符串內(nèi)的字符串。 比如 “3382” 就是 “this is a 3382 test” 的子字符串。 我們有多種方法可以從中把數(shù)字或指定部分字符串抽取出來(lái)。

How to Extract substring in Bash Shell on Linux or Unix

How to Extract substring in Bash Shell on Linux or Unix

本文會(huì)向你展示在 bash shell 中如何獲取或者說(shuō)查找出子字符串。 

在 Bash 中抽取子字符串

其語(yǔ)法為:

  1. ## 格式 ##
  2. ${parameter:offset:length}

子字符串?dāng)U展是 bash 的一項(xiàng)功能。它會(huì)擴(kuò)展成 parameter 值中以 offset 為開(kāi)始,長(zhǎng)為 length 個(gè)字符的字符串。 假設(shè), $u 定義如下:

  1. ## 定義變量 u ##
  2. u="this is a test"

那么下面參數(shù)的子字符串?dāng)U展會(huì)抽取出子字符串:

  1. var="${u:10:4}"
  2. echo "${var}"

結(jié)果為:

  1. test

其中這些參數(shù)分別表示:

  • 10 : 偏移位置
  • 4 : 長(zhǎng)度 

使用 IFS

根據(jù) bash 的 man 頁(yè)說(shuō)明:

IFS (內(nèi)部字段分隔符)用于在擴(kuò)展后進(jìn)行單詞分割,并用內(nèi)建的 read 命令將行分割為詞。默認(rèn)值是。

另一種 POSIX 就緒POSIX ready的方案如下:

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

輸出為:

  1. this
  2. is
  3. a
  4. test

下面是一段 bash 代碼,用來(lái)從 Cloudflare cache 中去除帶主頁(yè)的 url。

  1. #!/bin/bash
  2. ####################################################
  3. ## Author - Vivek Gite {https://www.cyberciti.biz/}
  4. ## Purpose - Purge CF cache
  5. ## License - Under GPL ver 3.x+
  6. ####################################################
  7. ## set me first ##
  8. zone_id="YOUR_ZONE_ID_HERE"
  9. api_key="YOUR_API_KEY_HERE"
  10. email_id="YOUR_EMAIL_ID_HERE"
  11.  
  12. ## hold data ##
  13. home_url=""
  14. amp_url=""
  15. urls="$@"
  16.  
  17. ## Show usage
  18. [ "$urls" == "" ] && { echo "Usage: $0 url1 url2 url3"; exit 1; }
  19.  
  20. ## Get home page url as we have various sub dirs on domain
  21. ## /tips/
  22. ## /faq/
  23.  
  24. get_home_url(){
  25. local u="$1"
  26. IFS='/'
  27. set -- $u
  28. echo "${1}${IFS}${IFS}${3}${IFS}${4}${IFS}"
  29. }
  30.  
  31. echo
  32. echo "Purging cache from Cloudflare。.。"
  33. echo
  34. for u in $urls
  35. do
  36. home_url="$(get_home_url $u)"
  37. amp_url="${u}amp/"
  38. curl -X DELETE "https://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache" \
  39. -H "X-Auth-Email: ${email_id}" \
  40. -H "X-Auth-Key: ${api_key}" \
  41. -H "Content-Type: application/json" \
  42. --data "{\"files\":[\"${u}\",\"${amp_url}\",\"${home_url}\"]}"
  43. echo
  44. done
  45. echo

它的使用方法為:

  1. ~/bin/cf.clear.cache https://www.cyberciti.biz/faq/bash-for-loop/ https://www.cyberciti.biz/tips/linux-security.html 

借助 cut 命令

可以使用 cut 命令來(lái)將文件中每一行或者變量中的一部分刪掉。它的語(yǔ)法為:

  1. u="this is a test"
  2. echo "$u" | cut -d' ' -f 4
  3. echo "$u" | cut --delimiter=' ' --fields=4
  4. ##########################################
  5. ## WHERE
  6. ## -d' ' : Use a whitespace as delimiter
  7. ## -f 4 : Select only 4th field
  8. ##########################################
  9. var="$(cut -d' ' -f 4 <<< $u)"
  10. echo "${var}"

想了解更多請(qǐng)閱讀 bash 的 man 頁(yè):

  1. man bash
  2. man cut

另請(qǐng)參見(jiàn): Bash String Comparison: Find Out IF a Variable Contains a Substring 

責(zé)任編輯:龐桂玉 來(lái)源: Linux中國(guó)
相關(guān)推薦

2022-12-06 08:27:50

Bash腳本字符串

2022-11-25 07:53:26

bash腳本字符串

2023-01-09 09:52:06

Bash字符串

2022-12-08 12:05:03

Bash字符串

2021-04-15 00:16:18

JavaString字符串

2022-05-10 09:47:10

Bash字符串Linux

2022-12-15 16:23:32

JavaScrip字符串開(kāi)發(fā)

2023-10-20 15:58:27

Python刪除指定字符

2009-11-24 19:33:07

PHP字符串中加入變量

2020-06-17 17:29:11

BashLinux

2022-11-21 12:06:24

fgrep命令Linux

2021-03-11 18:44:39

字符串SQL表達(dá)式

2024-12-04 17:05:14

find()函數(shù)字符串

2020-07-01 18:31:14

Linuxbash永遠(yuǎn)循環(huán)

2021-11-25 00:04:16

C# 插值字符串

2023-07-30 09:50:51

Bash字符串

2023-10-19 14:52:27

2022-11-24 08:01:57

bash腳本字符串

2010-06-28 15:18:51

SQL Server

2021-01-09 23:11:33

SQL數(shù)據(jù)庫(kù)字母
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)