如何在Bash中抽取子字符串

系统 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

How to Extract substring in Bash Shell on Linux or Unix

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

在 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中国
相关推荐

2022-12-06 08:27:50

Bash脚本字符串

2022-11-25 07:53:26

bash脚本字符串

2023-01-09 09:52:06

Bash字符串

2022-05-10 09:47:10

Bash字符串Linux

2021-04-15 00:16:18

JavaString字符串

2022-12-15 16:23:32

JavaScrip字符串开发

2022-12-08 12:05:03

Bash字符串

2023-10-20 15:58:27

Python删除指定字符

2020-06-17 17:29:11

BashLinux

2009-11-24 19:33:07

PHP字符串中加入变量

2021-03-11 18:44:39

字符串SQL表达式

2021-11-25 00:04:16

C# 插值字符串

2022-11-21 12:06:24

fgrep命令Linux

2023-10-19 14:52:27

2023-07-30 09:50:51

Bash字符串

2022-11-24 08:01:57

bash脚本字符串

2020-07-01 18:31:14

Linuxbash永远循环

2021-01-09 23:11:33

SQL数据库字母

2021-09-13 10:20:49

Python数据程序

2010-06-28 15:18:51

SQL Server
点赞
收藏

51CTO技术栈公众号