Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace specific word between characters using sed?

Tags:

regex

sed

Does anyone know how to replace specific words between (). So, for example, replace the word and with or in between both sets of parentheseis:

(grade='C' and grade='D' and grade='E') and (int_rate>=10 and int_rate<=20) and pub_rec>=0 and term=' 36 months'

This would be:

(grade='C' or grade='D' or grade='E') and (int_rate>=10 or int_rate<=20) and pub_rec>=0 and term=' 36 months'

like image 919
John Richardson Avatar asked Jun 03 '13 14:06

John Richardson


Video Answer


1 Answers

Really dorky approach:

$ sed 's/and/or/g' INPUT | sed 's/) or (/) and (/' | sed 's/\(.*\)or/\1and/'
(grade='C' or grade='D' or grade='E') and (int_rate>=10 or int_rate<=20) and pub_rec>=0 
and term=' 36 months'
like image 123
Fredrik Pihl Avatar answered Sep 22 '22 06:09

Fredrik Pihl