Given a regular expression and a replacement string, how can I use awk to implement the following:
- if a line contains no match, does nothing;
- if a line contains more than one matches, replace the last match with the replacement string, in the sense of the last match which would be substituted by
gsub()
; - if a line contains exactly one match, replace the match with the replacement string.
For example, see " unexpected character '\'" in gawk
Thanks.
sed 's/\(.*\)regex/\1replacement/'
should be enough.x{3,6}
because the greedy(.*)
could eat up to three of thex
characters and still be part of an overall valid RE.