我需要在变量$word
中包含<xml>
</xml>
和$line
的所有实例,但不要在字母数字字符出现在之前或之后。例如,给定此行,如果$word
设置为“fish”:
fish. The fish is a goldfish. Who likes fish?
这变为:
<xml>fish</xml>. The <xml>fish</xml> is a goldfish. Who likes <xml>fish</xml>?
这是我到目前为止用AWk尝试过的,但它不起作用:
echo "${line}" | awk '{
gsub(/^|[^[:alnum:]]*($word)*$|[^[:alnum:]]*/, "<xml>&</xml>");
}1'
答案 0 :(得分:2)
这个sed应该做的工作:
> s='fish. The fish is a goldfish. Who likes fish?'
> sed -r 's#(^|[^[:alnum:]])(fish)([^[:alnum:]]|$)#\1<xml>\2</xml>\3#g' <<< "$s"
<xml>fish</xml>. The <xml>fish</xml> is a goldfish. Who likes <xml>fish</xml>?
答案 1 :(得分:1)
你可能正在寻找单词边界,似乎无法找到一种简单的方法来使用gawk,但这里是一个perl版本:
echo 'fish. The fish is a goldfish. Who likes fish?' | perl -p -e 's/\b(fish)\b/<xml>\1<\/xml>/g'
<xml>fish</xml>. The <xml>fish</xml> is a goldfish. Who likes <xml>fish</xml>?
答案 2 :(得分:0)
echo "${line}" | sed s'@fish@<xml>fish</xml>@'g - My solution.