You can use double quotes here:
$ sed "s/'[^']*'/\"verified\"/g" ip.txt
Haarlem -m "verified" -n "verified" aalsmeer > "goes to"
Roterdam -m "verified" -n "verified" amsterdam > "goes to"
'[^']*'
matches '
followed by non '
characters and again '
character
- you cannot use
'.*'
because that will match from first '
to last '
in the input line
\"verified\"
use "verified"
as replacement string, double quote is escaped because it is being used inside double quotes
See also: https://mywiki.wooledge.org/Quotes and Why does my regular expression work in X but not in Y? (for difference between regex and wildcards)
The question was updated after above solution.
$ cat ip.txt
Haarlem -m 'foo' -n 'bar' aalsmeer > "goes to" -t 'value'
Roterdam -m 'foo2' -n 'bar2' amsterdam > "goes to" -t 'value'
$ sed -E "s/(-[mn] )'[^']*'/\1\"verified\"/g" ip.txt
Haarlem -m "verified" -n "verified" aalsmeer > "goes to" -t 'value'
Roterdam -m "verified" -n "verified" amsterdam > "goes to" -t 'value'
-[mn]
will match either -m
or -n
(-[mn] )
is a capture group, the content matched is reused in replacement section as \1
You can also use
sed -E "s/(-[^t] )'[^']*'/\1\"verified\"/g" ip.txt
to prevent only -t '..'
from getting replaced and match all other single quoted patterns preceded by -<char>
and a space
*
is not a wildcard in regex, but a quantifier. – Panki Jun 03 '20 at 12:17sed
command interfering with those in the regular expression you are using to match the string to be replaced. – AdminBee Jun 03 '20 at 12:20