我知道类似的问题已经被问了多次,但我似乎无法让我找到的任何解决方案起作用。我有一个文件中的一行看起来像这样:
<package foo="bar" unique-identifier="需要被替换的内容" version="foobar">
我需要替换上面指示的部分,但我似乎总是改变不想改变的信息。需要替换的文本因文件而异,所以我目前在它的位置使用通配符。以下是我尝试过的方法。
sed 's/unique\-identifier\=\".*\"/unique\-identifier\=\"新文本\"/g' $file >> $newFile
sed 's/\bunique\-identifier\=\".*\"\b/unique\-identifier\=\"新文本\"/g' $file >> $newFile
sed 's/\<unique\-identifier\=\".*\"\>/unique\-identifier\=\"新文本\"/g' $file >> $newFile
sed 's/[[:<:]]unique\-identifier\=\".*\"[[:>:]]/unique\-identifier\=\"新文本\"/g' $file >> $newFile
这些都是我找到的各种解决方案,最后一个是Mac OSX特定的。理想情况下,我想只替换双引号中的内容,但我的正则表达式技能有限。
*
is greedy! - Karoly Horvathxmlstarlet ed -u '//package/@unique-identifier[.="NEEDS_TO_BE_REPLACED"]' -v 'NEW_TEXT'
这样的命令。 - Wintermute