Search code examples
regexlinuxawk

Substitute a regex pattern using awk


I am trying to write a regex expression to replace one or more '+' symbols present in a file with a space. I tried the following:

 echo This++++this+++is+not++done | awk '{ sub(/\++/, " "); print }'
 This this+++is+not++done

Expected:

This this is not done

Any ideas why this did not work?


Solution

  • Use gsub which does global substitution:

    echo This++++this+++is+not++done | awk '{gsub(/\++/," ");}1'
    

    sub function replaces only 1st match, to replace all matches use gsub.