Home > System Tutorial > LINUX > body text

Extract substring under Bash

王林
Release: 2024-03-04 09:02:05
forward
453 people have browsed it

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

在 Bash 中抽取子字符串

其语法为:

## 格式 ##
${parameter:offset:length}
Copy after login

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

## 定义变量 u ##
u="this is a test"
Copy after login

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

var="${u:10:4}"
echo "${var}"
Copy after login

结果为:

test
Copy after login

其中这些参数分别表示:

10 : 偏移位置
4 : 长度

使用 IFS

根据 bash 的 man 页说明:

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

另一种 POSIX 就绪POSIX ready的方案如下:

u="this is a test"
set -- $u
echo "$1"
echo "$2"
echo "$3"
echo "$4"
Copy after login

输出为:

this
is
a
test
Copy after login

下面是一段 bash 代码,用来从 Cloudflare cache 中去除带主页的 url。

#!/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 -- $u
echo "${1}${IFS}${IFS}${3}${IFS}${4}${IFS}"
}
echo
echo "Purging cache from Cloudflare。.。"
echo
for u in $urls
do
home_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}\"]}"
echo
done
echo
Copy after login

它的使用方法为:

~/bin/cf.clear.cache https://www.cyberciti.biz/faq/bash-for-loop/ 
https://www.cyberciti.biz/tips/linux-security.html
Copy after login
借助 cut 命令

可以使用 cut 命令来将文件中每一行或者变量中的一部分删掉。它的语法为:

u="this is a test"
echo "$u" | cut -d' ' -f 4
echo "$u" | cut --delimiter=' ' --fields=4
##########################################
## WHERE
## -d' ' : Use a whitespace as delimiter
## -f 4 : Select only 4th field
##########################################
var="$(cut -d' ' -f 4 
<p>想了解更多请阅读 bash 的 man 页:</p>
<pre class="brush:php;toolbar:false">man bash
man cut
Copy after login

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

The above is the detailed content of Extract substring under Bash. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template