使用 gsub 进行模式匹配

问题描述 投票:0回答:1

我正在尝试搜索和替换字符串的多个模式,并将其替换为不同的值。然而,该命令仅替换一种模式并忽略其余模式。

output=$(echo "$output4" | awk 'NR>1 {gsub(/\[|]/,""); gsub("+r","READ"); gsub("+r+w","READ and WRITE"); gsub("+r+w+g","READ, WRITE and GRANT"); print}')

当我执行脚本时,这就是我得到的。

    Group Analyst_Corp in security domain Native: READ. Group Finance_Dev in security domain Native: READ+w. Manufacturing_Corp in security domain Native: READ+w
Information_Tech in security domain Native: READ+w+g.

我正在寻找这样的输出。

        Group Analyst_Corp in security domain Native: READ. Group Finance_Dev in security domain Native: READ and WRITE. Manufacturing_Corp in security domain Native: READ and WRITE
    Information_Tech in security domain Native: READ, WRITE and GRANT.
shell awk gsub
1个回答
0
投票

按长度降序替换重叠的图案。否则替换完

+r+w
后就找不到
+r
了。

awk 'NR>1 {
    gsub(/\[|]/,""); 
    gsub("+r+w+g","READ, WRITE and GRANT"); 
    gsub("+r+w","READ and WRITE"); 
    gsub("+r","READ"); 
    print}'
© www.soinside.com 2019 - 2024. All rights reserved.