使用 sed 删除开头和结尾的一些字符
Remove some character in the start and in the end using sed
我正在尝试提取 "profile "
和 "]"
之间的单词。
我的内容
[profile gateway]
[profile personal]
[profile DA]
[profile CX]
为此我试过了
less ~/.aws/config |grep "\[profile"|sed -E 's/\[profile(.)//'
这给出了
gateway]
personal]
DA]
CX]
我知道可以添加一个管道和我们 tr 来删除最后一个 "]"
甚至 cut 都可以但是有人可以用上面的 sed 命令帮助我用正则表达式删除最后一个 "]"
您可以使用sed
:
sed -n 's/.*\[profile *\([^][]*\).*//p' ~/.aws/config
详情:
-n
- 抑制默认行输出
.*\[profile *\([^][]*\).*/
- 找到任何文本,[profile
,零个或多个空格,然后将 [
和 ]
以外的任何零个或多个字符捕获到组 1 中,然后匹配其余的文本
</code> - 替换为第 1 组值</li>
<li><code>p
- 打印替换结果。
看到一个 online demo:
s='[profile gateway]
[profile personal]
[profile DA]
[profile CX]'
sed -n 's/.*\[profile *\([^][]*\).*//p' <<< "$s"
输出:
gateway
personal
DA
CX
使用 GNU grep
grep -oP '(?<=\[profile )[^]]+' ~/.aws/config
(?<=\[profile )[^]]+
正则表达式匹配紧接在 profile
字符串之前的位置,然后匹配 ]
以外的一个或多个字符。 -o
选项使 grep
仅提取匹配项,P
启用 PCRE 正则表达式语法。
和awk
您也可以使用 awk
:
awk '/^\[profile .*]$/{print substr(, 0, length()-1)}' ~/.aws/config
它将找到所有以 [profile
开头的行,并输出没有最后一个字符的第二个字段(即 ]
将被省略的字符)。
如果您可以将 grep 与 -P
一起用于 Perl 兼容的正则表达式:
less ~/.aws/config | grep -oP "\[profile \K[^][]+(?=])"
模式匹配:
\[profile
字面匹配
\K
忘记目前匹配的是什么
[^][]+
匹配除 [
和 ]
之外的任何字符 1+ 次
(?=])
肯定前瞻断言(不匹配)]
对于示例内容,输出将是
gateway
personal
DA
CX
-- extract word between profile
and ]
表示删除最多profile
和从]
开始, IE。 ^.*profile
和 ].*$
:
$ sed 's/^.*profile \|\].*$//g' file
输出:
gateway
personal
DA
CX
注意,如果只找到一个边界,则将其删除。
在awk
中保持简单;通过将字段分隔符设置为 [profile
或 ]
(根据所示示例)并根据需要的输出打印列。
awk -F'\[profile |\]' '{print }' Input_file
另一个较短的awk
解决方案:
awk -F '[] ]' ' == "[profile" {print }' ~/.aws/config
gateway
personal
DA
CX
trying to extract word between "profile " and "]"
还使用 awk
作为条件 profile
在 </code> 的末尾:</p>
<pre><code>awk ' ~ /profile$/ {sub(/]$/,"",);print }' file
gateway
personal
DA
CX
我正在尝试提取 "profile "
和 "]"
之间的单词。
我的内容
[profile gateway]
[profile personal]
[profile DA]
[profile CX]
为此我试过了
less ~/.aws/config |grep "\[profile"|sed -E 's/\[profile(.)//'
这给出了
gateway]
personal]
DA]
CX]
我知道可以添加一个管道和我们 tr 来删除最后一个 "]"
甚至 cut 都可以但是有人可以用上面的 sed 命令帮助我用正则表达式删除最后一个 "]"
您可以使用sed
:
sed -n 's/.*\[profile *\([^][]*\).*//p' ~/.aws/config
详情:
-n
- 抑制默认行输出.*\[profile *\([^][]*\).*/
- 找到任何文本,[profile
,零个或多个空格,然后将[
和]
以外的任何零个或多个字符捕获到组 1 中,然后匹配其余的文本</code> - 替换为第 1 组值</li> <li><code>p
- 打印替换结果。
看到一个 online demo:
s='[profile gateway]
[profile personal]
[profile DA]
[profile CX]'
sed -n 's/.*\[profile *\([^][]*\).*//p' <<< "$s"
输出:
gateway
personal
DA
CX
使用 GNU grep
grep -oP '(?<=\[profile )[^]]+' ~/.aws/config
(?<=\[profile )[^]]+
正则表达式匹配紧接在 profile
字符串之前的位置,然后匹配 ]
以外的一个或多个字符。 -o
选项使 grep
仅提取匹配项,P
启用 PCRE 正则表达式语法。
和awk
您也可以使用 awk
:
awk '/^\[profile .*]$/{print substr(, 0, length()-1)}' ~/.aws/config
它将找到所有以 [profile
开头的行,并输出没有最后一个字符的第二个字段(即 ]
将被省略的字符)。
如果您可以将 grep 与 -P
一起用于 Perl 兼容的正则表达式:
less ~/.aws/config | grep -oP "\[profile \K[^][]+(?=])"
模式匹配:
\[profile
字面匹配\K
忘记目前匹配的是什么[^][]+
匹配除[
和]
之外的任何字符 1+ 次
(?=])
肯定前瞻断言(不匹配)]
对于示例内容,输出将是
gateway
personal
DA
CX
-- extract word between profile
and ]
表示删除最多profile
和从]
开始, IE。 ^.*profile
和 ].*$
:
$ sed 's/^.*profile \|\].*$//g' file
输出:
gateway
personal
DA
CX
注意,如果只找到一个边界,则将其删除。
在awk
中保持简单;通过将字段分隔符设置为 [profile
或 ]
(根据所示示例)并根据需要的输出打印列。
awk -F'\[profile |\]' '{print }' Input_file
另一个较短的awk
解决方案:
awk -F '[] ]' ' == "[profile" {print }' ~/.aws/config
gateway
personal
DA
CX
trying to extract word between "profile " and "]"
还使用 awk
作为条件 profile
在 </code> 的末尾:</p>
<pre><code>awk ' ~ /profile$/ {sub(/]$/,"",);print }' file
gateway
personal
DA
CX