如何使用awk打印匹配的正则表达式模式? [英] How to print matched regex pattern using awk?

查看:475
本文介绍了如何使用awk打印匹配的正则表达式模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用awk,我需要在与正则表达式模式匹配的文件中找到一个单词.

Using awk, I need to find a word in a file that matches a regex pattern.

只想打印与模式匹配的单词.

I only want to print the word matched with the pattern.

因此,如果在一行中,我有:

So if in the line, I have:

xxx yyy zzz

和模式:

/yyy/

我只想得到:

yyy


感谢 kurumi ,我设法写了这样的东西:


thanks to kurumi i managed to write something like this:

awk '{
        for(i=1; i<=NF; i++) {
                tmp=match($i, /[0-9]..?.?[^A-Za-z0-9]/)
                if(tmp) {
                        print $i
                }
        }
}' $1

这就是我所需要的:)非常感谢!

and this is what i needed :) thanks a lot!

推荐答案

这是非常基本的

awk '/pattern/{ print $0 }' file

询问awk,以使用//搜索pattern,然后打印出该行,默认情况下,该行称为记录,用$ 0表示.至少阅读文档.

ask awk to search for pattern using //, then print out the line, which by default is called a record, denoted by $0. At least read up the documentation.

如果您只想打印出匹配的单词.

If you only want to get print out the matched word.

awk '{for(i=1;i<=NF;i++){ if($i=="yyy"){print $i} } }' file

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

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