用sed替换字符之间的特定单词?

时间:2023-01-07 16:48:39

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'

(grade ='C'和grade ='D'和grade ='E')和(int_rate> = 10且int_rate <= 20)和pub_rec> = 0且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'

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

6 个解决方案

#1


1  

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'

#2


1  

This might work for you (GNU sed):

这可能适合你(GNU sed):

sed -r ':a;s/\band\b([^()]*\))/or\1/;ta' file

Work backwards throught the file changing and's within (...) to or's.

通过文件更改向后工作,并在(...)到或之内。

#3


1  

With perl

用perl

#!/usr/bin/perl

$_ ="(grade='C' and grade='D' and grade='E') and (int_rate>=10 and int_rate<=20) and pub_rec>=0 and term=' 36 months'";
print "Before: $_\n";

while (/(\([^\)]+?)and(.+?\))/) {
    s/(\([^\)]+?)and(.+?\))/$1or$2/g;
}
print "After: $_\n";

#4


0  

I thought about it a bit more and came up with this solution:

我想了一下,想出了这个解决方案:

$ sed "s/(/\n(/g" | sed "s/)/)\n/g" | sed "/(/s/and/or/g" | tr '\n' '!' | sed "s/!//g"
(grade='C' or grade='D' or grade='E') and (int_rate>=10 or int_rate<=20) and pub_rec>=0 and term=' 36 months'

Basically, broke up content in multiple lines with with separator as parenthesis, use sed on all lines with parenthesis to convert all ands to ors, then bring back all into single line.

基本上,使用分隔符作为括号分解多行中的内容,在带括号的所有行上使用sed将所有ands转换为ors,然后将all全部转换为单行。

#5


0  

One solution using

一个使用vim的解决方案

Content of script.vim

script.vim的内容

set backup
while search( ')', 'W' ) > 0 
    execute "normal vi)\<ESC>"
    '<,'>s/\v%V<and>/or/ge
    call cursor( line('.'), col('.') + 2 )
endwhile
normal ZZ

Run it like:

运行它像:

vim -S script.vim infile

That updates the file in-place with following result.

使用以下结果就地更新文件。

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

Also creates a backup file appending the ~ suffix to the original file name.

还会创建一个备份文件,将〜后缀附加到原始文件名。

#6


0  

I don't know Sed, but here is a regexp that matches only and if followed by a ):

我不知道Sed,但这里只是匹配的正则表达式,如果后跟a):

\band\b(?=[^(]*\))

Simply replace the matches by or just as this demo.

只需将此匹配替换为此演示。


Side note: this won't check the syntax of the input, i.e. the number of parentheses, etc. If you intend to do so, maybe some existing parsers could suit your needs.

旁注:这不会检查输入的语法,即括号的数量等。如果您打算这样做,也许一些现有的解析器可以满足您的需求。

#1


1  

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'

#2


1  

This might work for you (GNU sed):

这可能适合你(GNU sed):

sed -r ':a;s/\band\b([^()]*\))/or\1/;ta' file

Work backwards throught the file changing and's within (...) to or's.

通过文件更改向后工作,并在(...)到或之内。

#3


1  

With perl

用perl

#!/usr/bin/perl

$_ ="(grade='C' and grade='D' and grade='E') and (int_rate>=10 and int_rate<=20) and pub_rec>=0 and term=' 36 months'";
print "Before: $_\n";

while (/(\([^\)]+?)and(.+?\))/) {
    s/(\([^\)]+?)and(.+?\))/$1or$2/g;
}
print "After: $_\n";

#4


0  

I thought about it a bit more and came up with this solution:

我想了一下,想出了这个解决方案:

$ sed "s/(/\n(/g" | sed "s/)/)\n/g" | sed "/(/s/and/or/g" | tr '\n' '!' | sed "s/!//g"
(grade='C' or grade='D' or grade='E') and (int_rate>=10 or int_rate<=20) and pub_rec>=0 and term=' 36 months'

Basically, broke up content in multiple lines with with separator as parenthesis, use sed on all lines with parenthesis to convert all ands to ors, then bring back all into single line.

基本上,使用分隔符作为括号分解多行中的内容,在带括号的所有行上使用sed将所有ands转换为ors,然后将all全部转换为单行。

#5


0  

One solution using

一个使用vim的解决方案

Content of script.vim

script.vim的内容

set backup
while search( ')', 'W' ) > 0 
    execute "normal vi)\<ESC>"
    '<,'>s/\v%V<and>/or/ge
    call cursor( line('.'), col('.') + 2 )
endwhile
normal ZZ

Run it like:

运行它像:

vim -S script.vim infile

That updates the file in-place with following result.

使用以下结果就地更新文件。

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

Also creates a backup file appending the ~ suffix to the original file name.

还会创建一个备份文件,将〜后缀附加到原始文件名。

#6


0  

I don't know Sed, but here is a regexp that matches only and if followed by a ):

我不知道Sed,但这里只是匹配的正则表达式,如果后跟a):

\band\b(?=[^(]*\))

Simply replace the matches by or just as this demo.

只需将此匹配替换为此演示。


Side note: this won't check the syntax of the input, i.e. the number of parentheses, etc. If you intend to do so, maybe some existing parsers could suit your needs.

旁注:这不会检查输入的语法,即括号的数量等。如果您打算这样做,也许一些现有的解析器可以满足您的需求。