awk gsub

Discussion in 'Programming/Scripts' started by blocker, Nov 19, 2008.

  1. blocker

    blocker Member


    is it possible to replace exact words with awk and gsub?

    The command:

    /usr/bin/awk '{gsub(/master/,"slave");print}' /tmp/test > /tmp/test1.temp
    does replace ALL master to slave, but it also replaces blahblahmaster to blahblahslave, which i do not want...

  2. burschik

    burschik New Member

    You know how regular expressions work, right? You know the meaning of "the empty string at either the beginning or the end of a word", right?
  3. blocker

    blocker Member

    thanks for reminding

    /usr/bin/awk '{gsub(/ master/," slave");print}' /tmp/test > /tmp/test1.temp
    does do the job
  4. burschik

    burschik New Member

    You are welcome. As an aside, " " is not the empty string, and many implementations of regular expressions (including that of GNU awk) have a particular syntax for the empty string at the beginning and/or end of a word, i.e. the transition between word characters and non-word characters.

Share This Page