如何查找和替换AWK,但不能在前后字符是字母数字时?

时间:2013-11-23 10:52:19

标签: bash replace awk

我需要在变量$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'

3 个答案:

答案 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.