使用awk仅捕获字符串中的数字 [英] Using awk to grab only numbers from a string

查看:1064
本文介绍了使用awk仅捕获字符串中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
我有一列应该应以描述文本ref12345678"的形式获取用户输入.我现有的脚本可以获取参考号,但不幸的是,有些用户错误地添加了参考号,因此可以代替"ref12345678""RF12345678""abcd12345678"或其他任何形式.自然,错误的格式会破坏某些触发的脚本. 现在,我无法控制用户对此字段的输入,因此我想稍后在管道中制作脚本以获取编号.

Background:
I have a column that should get user input in form of "Description text ref12345678". I have existing scripts that grab the reference number but unfortunately some users add it incorrectly so instead of "ref12345678" it can be "ref 12345678", "RF12345678", "abcd12345678" or any variation. Naturally the wrong formatting breaks some of the triggered scripts. For now I can't control the user input to this field, so I want to make the scripts later in the pipeline just to get the number.

此刻,我要用awk '{gsub(/[[:alpha:]]/, "")}; 1'剥离字母,但是替换似乎是一种无效的解决方案. (我知道我也可以使用sed -n 's/.*[a-zA-Z]//p'tr -d '[[:alpha:]]'来做到这一点,但是它们本质上是相同的,我希望awk可以实现额外的可编程性.)

At the moment I'm stripping the letters with awk '{gsub(/[[:alpha:]]/, "")}; 1', but substitution seems like an inefficient solution. (I know I can do this also with sed -n 's/.*[a-zA-Z]//p' and tr -d '[[:alpha:]]' but they are essentially the same and I want awk for additional programmability).

问题是,是否有一种方法可以将awk设置为仅打印字符串中的数字,或者将分隔符设置为字符串中的数字项? (或者替代确实是解决此问题的最有效方法).

The question is, is there a way to set awk to either print only numbers from a string, or set delimits to numeric items in a string? (or is substitution really the most efficient solution for this problem).

因此,总而言之:如何在$ echo "ref12345678"中使用awk来仅打印"12345678"而不进行替换?

So in summary: how do I use awk for $ echo "ref12345678" to print only "12345678" without substitution?

推荐答案

如果awk 不是必须的:

grep -o '[0-9]\+'

示例:

kent$ echo "ref12345678"|grep -o '[0-9]\+'
12345678

awk 为例:

kent$ echo "ref12345678"|awk -F'[^0-9]*' '$0=$2'     
12345678

这篇关于使用awk仅捕获字符串中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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