shellawkgsub

pattern matching using gsub


I am trying to search and replace multiple patterns of a string and replace it with different values. However the command is replacing only one pattern and ignoring the rest.

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}')

When I execute the script, this is what I am getting.

    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.

I am looking for the output like this.

        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.

Solution

  • Guessing at OP's input (ie, contents of the output4 variable), and throwing in some [ and ] characters for good measure:

    output4="Some line to ignore
        Group [Analyst_Corp] in security domain [Native]: +r. Group [Finance_Dev] in security domain [Native]: +r+w. [Manufacturing_Corp] in security domain [Native]: +r+w
    [Information_Tech] in security domain [Native]: +r+w+g."
    

    There are a few issues with the current code:

    A few different options for modifying OP's current code:

    ########
    # static regex; using `\` to escape the '+'
    
    awk 'NR>1 { gsub(/[][]/,"")                             # remove brackets '[' and ']'
                gsub(/\+r\+w\+g/,"READ, WRITE and GRANT")
                gsub(/\+r\+w/,"READ and WRITE")
                gsub(/\+r/,"READ")
                print
              }
    ' <<< "${output4}"
    
    ########
    # dynamic regex; using '[]' to escape the '+'
    
    awk 'NR>1 { gsub(/[][]/,"")
                gsub("[+]r[+]w[+]g","READ, WRITE and GRANT")
                gsub("[+]r[+]w","READ and WRITE")
                gsub("[+]r","READ")
                print
              }
    ' <<< "${output4}"
    
    ########
    # static regex; using '[]' to escape the '+'
    
    awk 'NR>1 { gsub(/[][]/,"")
                gsub(/[+]r[+]w[+]g/,"READ, WRITE and GRANT")
                gsub(/[+]r[+]w/,"READ and WRITE")
                gsub(/[+]r/,"READ")
                print
              }
    ' <<< "${output4}"
    

    All of these generate:

        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.
    

    Collapsing down to one-liners and storing results in variable output:

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

    All of these generate:

    $ typeset -p output
    declare -- output="        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."