awk-删除正则表达式中的字符 [英] awk - remove character in regex

查看:456
本文介绍了awk-删除正则表达式中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从此正则表达式中用awk删除1:如果在任何字段中找到了所述正则表达式,则为^1[0-9]{10}$.我已经尝试使它与subsubstr一起工作了几个小时,但我无法为此找到正确的逻辑.我已经有了sed的解决方案:s/^1\([0-9]\{10\}\)$/\1/,我需要使用awk进行此工作.

I want to remove 1 with awk from this regex: ^1[0-9]{10}$ if said regex is found in any field. I've been trying to make it work with sub or substr for a few hours now, I am unable to find the correct logic for this. I already have the solution for sed: s/^1\([0-9]\{10\}\)$/\1/, I need to make this work with awk.

编辑输入和输出示例.输入:

Edit for input and output example. Input:

10987654321
2310987654321
1098765432123    

(awk扭曲和过于复杂的语法)

(awk twisted and overcomplicated syntax)

输出:

0987654321
2310987654321
1098765432123    

基本上,仅当前导1后跟十位数字时才需要将其删除.第2和第3个示例行是正确的,第2个在1前面有23,第3个有一个前导1,但是后面是12位而不是10位.这就是正则表达式指定的内容.

Basically the leading 1 needs to be removed only when it's followed by ten digits. The 2nd and 3rd example lines are correct, 2nd has 23 in front of 1, 3rd has a leading 1 but it's followed by 12 digits instead of ten. That's what the regex specifies.

推荐答案

使用sub(),您可以尝试:

awk '/^1[0-9]{10}$/ { sub(/^1/, "") }1' file

或使用substr():

awk '/^1[0-9]{10}$/ { $0 = substr($0, 2) }1' file

如果您需要测试每个字段,请尝试遍历它们:

If you need to test each field, try looping over them:

awk '{ for(i=1; i<=NF; i++) if ($i ~ /^1[0-9]{10}$/) sub(/^1/, "", $i) }1' file

https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html

这篇关于awk-删除正则表达式中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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