I have a file containing hundreds of values that start with $
.
$ cat /tmp/file
$one $t $three
$one $t $three $t $three
I'm trying to use sed
to replace only the values that start with $t
.
$ sed "s/\$t/foo/g" /tmp/file
$one foo foohree
$one foo foohree foo foohree
But the above command replaces the $three
values as well. How can I prevent this?
'
) to quote regular expressions on the command line. Typical shells turn"\$"
(with double quotes) into the literal$
which is a meta-character for the end-of-string in sed regular expressions. It just so happens that sed interprets it as a literal$
if it occurs at the beginning of a regular expression literal. However, if you write'\$'
, it becomes\$
and sed will know that you want to match the literal$
character (and not end-of-string) regardless of its position in the regular expression. – David Foerster Aug 8 '20 at 7:23