unix命令awk是否具有类似java.util.regex.Matcher.group(int)的功能?

时间:2013-04-29 07:41:23

标签: java regex unix

匹配整条线不能满足我的要求。我想匹配下面的特定行。

Pattern pattern = Pattern.compile("(.*you\\[)(\\w+\\-\\w+\\-\\w+)(\\]\\:\\slove\\:\\s)(.*)");
Matcher matcher = pattern .matcher(line);
System.out.println(matcher.group(2));

我想知道如何使用awk或其他Unix命令来执行此操作。我想匹配特定的组,例如

his number is 123 and his name is jack.

我希望在成功匹配整行后获得123jack。这是上面的正则表达式

(.*\\s)(\d+\s)(.*)(\\s.*)

我可以使用组(2)获得数字。但是如何使用awk获取它?

2 个答案:

答案 0 :(得分:1)

 echo "his number is 123 and his name is jack" | perl -ne 'print "$1\n$2\n$3\n$4" if /(.*\s)(\d+\s)(.*?)\s([^\s]*$)/'

会给出以下输出:


his number is 
123 
and his name is
jack

这里$ 2和$ 4分别包含数字和名称。

答案 1 :(得分:0)

awk '{print $4,$9}' FPAT='[0-9a-z]*'