Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substitute a regex pattern using awk

Tags:

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?

like image 975
Ajay Nair Avatar asked Jan 21 '13 03:01

Ajay Nair


2 Answers

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.

like image 110
Guru Avatar answered Sep 22 '22 17:09

Guru


Or the tr command:

echo This++++this+++is+not++done | tr -s '+' ' ' 
like image 22
radical7 Avatar answered Sep 21 '22 17:09

radical7