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.
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:
+r+w+g
) before replacing the shorter strings (eg, +r
)+
has a special meaning in regex patterns so to match on a literal +
we need to escape it (\+
or [+]
)\
you'll run into fewer issues if you work with a static regex (pattern wrapped in a pair of /
characters)[]
you can use a dynamic (pattern wrapped in double quotes) or static regex[
, ]
) we'll swap in a different regex that's a tad easier (?) to read/understandA 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."