当前位置:首页 >娱乐 >如何在Bash中抽取子字符串

如何在Bash中抽取子字符串

2024-07-01 10:29:39 [百科] 来源:避面尹邢网

如何在Bash中抽取子字符串

作者:Vivek Gite 系统 Linux 系统运维 所谓“子字符串”就是中字符出现在其它字符串内的字符串。 比如 “3382” 就是抽取串 “this is a 3382 test” 的子字符串。 本文会向你展示在 bash shell 中如何获取或者说查找出子字符串。中字符

所谓“子字符串”就是抽取串出现在其它字符串内的字符串。 比如 “3382” 就是中字符 “this is a 3382 test” 的子字符串。 我们有多种方法可以从中把数字或指定部分字符串抽取出来。抽取串

How to Extract substring in Bash Shell on Linux or Unix

如何在Bash中抽取子字符串

How to Extract substring in Bash Shell on Linux or Unix

如何在Bash中抽取子字符串

本文会向你展示在 bash shell 中如何获取或者说查找出子字符串。中字符 

如何在Bash中抽取子字符串

在 Bash 中抽取子字符串

其语法为:

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

子字符串扩展是抽取串 bash 的一项功能。它会扩展成 parameter值中以 offset为开始,中字符长为 length个字符的抽取串字符串。 假设,中字符 $u定义如下:

  1. ## 定义变量 u ##
  2. u="this 抽取串is a test"

那么下面参数的子字符串扩展会抽取出子字符串:

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

结果为:

  1. test

其中这些参数分别表示:

  • 10 : 偏移位置
  • 4 : 长度 

使用 IFS

根据 bash 的 man 页说明:

IFS (内部字段分隔符)用于在扩展后进行单词分割,并用内建的中字符 read 命令将行分割为词。默认值是抽取串。

另一种 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 代码,用来从 Cloudflare cache 中去除带主页的 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命令来将文件中每一行或者变量中的一部分删掉。它的语法为:

  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}"

想了解更多请阅读 bash 的 man 页:

  1. man bash
  2. man cut

另请参见: Bash String Comparison: Find Out IF a Variable Contains a Substring 

责任编辑:庞桂玉 来源: Linux中国 LinuxBash子字符串

(责任编辑:知识)

    推荐文章
    热点阅读