使用awk替换正则表达式模式 [英] Substitute a regex pattern using awk

查看:174
本文介绍了使用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

预期:

This this is not done

有什么想法为什么行不通?

Any ideas why this did not work?

推荐答案

使用gsub进行全局替换:

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

sub函数仅替换第一个匹配项,要替换所有匹配项,请使用gsub.

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

这篇关于使用awk替换正则表达式模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆