Bash 在子字符串之后和子字符串之前提取

Bik*_*Bik 3 string bash grep expression sed

假设我有一个字符串:

random text before authentication_token = 'pYWastSemJrMqwJycZPZ', gravatar_hash = 'd74a97f
Run Code Online (Sandbox Code Playgroud)

我想要一个 shell 命令来提取下一个 之前"authentication_token = '"和之前的所有内容'

所以基本上,我想回来pYWastSemJrMqwJycZPZ

我该怎么做呢?

cho*_*oba 5

使用参数扩展:

#!/bin/bash
text="random text before authentication_token = 'pYWastSemJrMqwJycZPZ', gravatar_hash = 'd74a97f"
token=${text##* authentication_token = \'}   # Remove the left part.
token=${token%%\'*}                          # Remove the right part.
echo "$token"
Run Code Online (Sandbox Code Playgroud)

请注意,即使随机文本包含authentication token = '...'.