I know that some form of this question has been asked multiple times over, but I can't seem to get any of the solutions I've found to work. I have a line in a file that looks something like:
<package foo="bar" unique-identifier="NEEDS_TO_BE_REPLACED" version="foobar">
I need to replace the part indicated above, but I seem to always change unwanted information as well. The text that needs to be replaced is different from file to file, so currently I'm using a wildcard in it's place. Below are the things I've tried.
sed 's/unique\-identifier\=\".*\"/unique\-identifier\=\"NEW_TEXT\"/g' $file >> $newFile
sed 's/\bunique\-identifier\=\".*\"\b/unique\-identifier\=\"NEW_TEXT\"/g' $file >> $newFile
sed 's/\<unique\-identifier\=\".*\"\>/unique\-identifier\=\"NEW_TEXT\"/g' $file >> $newFile
sed 's/[[:<:]]unique\-identifier\=\".*\"[[:>:]]/unique\-identifier\=\"NEW_TEXT\"/g' $file >> $newFile
All of those are various solutions that I've found, the last being Mac OSX specific. Ideally I'd like to be able to replace just what's in the double-quotes, but my regex skills are lacking.
You can try:
The reason your first attempt failed is because
.*
is greedy and matches as much as it can provided that the rest of the expression can be matched as well. In this case it matched also" version="foobar
, and the rest of the expression (\"
) matched the last character.Replacing
.*
with[^"]
makes this part match everything that is not"
.You could also remove repetitions and unnecessary escapes in your expression to make it more readable, like this: