Skip to content
Advertisement

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement