Linux grep 正则应用

福尔摩斯对我笑 2019-02-22 20532 次 7709条

在快下班的半个小时前,一基友@了我的微信,让我帮他写个grep提取字符的小功能。唉,又是一个表现自己的机会,我得抓住。

给了一个文件,内容大致如下

CO2:177 PPM/10.O2:20.3636 % [2019-02-22 12:04:07.120]
CO2:177 PPM/10.O2:20.3636 %  [2019-02-22 12:04:07.244]
CO2:167 PPM/10.O2:20.3235 % [2019-02-22 12:04:08.130]
CO2:167 PPM/10.O2:20.3235 %  [2019-02-22 12:04:08.243]
CO2:174 PPM/10.O2:20.2766 % [2019-02-22 12:04:09.140]
CO2:174 PPM/10.O2:20.2766 %  [2019-02-22 12:04:09.244]
CO2:162 PPM/10.O2:20.3435 % [2019-02-22 12:04:10.150]
CO2:162 PPM/10.O2:20.3435 %  [2019-02-22 12:04:10.244]

需求,提取CO2:和PPM之间的数字,还有O2:和%之间的数字。

利用正则

我比较笨,所以喜欢用拆解法,先进行初步提取

CO2:.*PPM //匹配结果为 CO2:177 PPM

他要的是之间的数字,所以这里进行第二步处理,需要用到零宽断言了。


(?<=CO2:).*(?=PPM)  //得出结果为177

基本完成第一个规则,后面也比较简单


(?<=CO2:).*(?=PPM)|(?<=\.O2:).*(?=%) //得出结果为:177 20.3636

 

grep 进行输出

先列下grep的参数 grep: 
传统的grep程序,在没有参数的情况下,只输出符合RE字符串之句子.常见参数如下: 
-v:逆反模示,只输出"不含" RE字符串之句子. 
-r:递归模式,可同时处理所有层级子目录里的文件. 
-q:静默模式,不输出任何结果(stderr除外.常用以获取return value,符合为true,否则为false .) 
-i:忽略大小写. 
-w:整词比对,类似\<word\> . 
-n:同时输出行号. 
-c:只输出符合比对的行数. 
-l:只输出符合比对的文件名称. 
-o:只输出符合RE的字符串. (gnu新版独有,不见得所有版本都支持.) -E:切换为egrep .
-P表明后面的pattern是perl兼容正则表达式,因为pcre才支持零宽断言 
grep -P '(?<=CO2:).*(?=PPM)|(?<=\.O2:).*(?=%)' regular_express.txt 

//结果如下,成功匹配

接着利用-o只输出符合的字符串

grep -oP '(?<=CO2:).*(?=PPM)|(?<=\.O2:).*(?=%)' regular_express.txt

可以看到,已经完成了。

奇怪的是:这条命令我在我的linux下可以正常运行。在基友MAC的terminal下,却无法正常使用,查了较多资料,还没搞明白,后续再更。

标签: /

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 7709 条评论 1


  1. golden goose

    Needed to send you a bit of note to help say thanks a lot once again about the remarkable thoughts you have provided here. It was simply unbelievably open-handed of people like you to offer extensively exactly what some people would’ve supplied for an ebook to make some cash for themselves, primarily seeing that you might have done it in the event you desired. These good tips also worked to become fantastic way to know that other individuals have a similar passion just as my personal own to learn a lot more with reference to this condition. Certainly there are a lot more pleasurable occasions ahead for people who read your blog.

    回复 2019年10月14日
  2. birkin bag

    I not to mention my buddies were found to be studying the excellent secrets and techniques on your web site then instantly came up with a terrible suspicion I had not expressed respect to the site owner for those secrets. All of the boys are already absolutely passionate to read through them and have now in fact been tapping into those things. Appreciation for really being considerably accommodating and also for utilizing this kind of superb tips most people are really needing to learn about. My very own sincere regret for not saying thanks to earlier.

    回复 2019年10月13日
  3. christian louboutin outlet

    I simply wanted to write a quick comment in order to say thanks to you for some of the marvelous tips and tricks you are placing on this website. My time consuming internet look up has now been recognized with wonderful facts and strategies to talk about with my visitors. I would point out that most of us readers actually are unequivocally endowed to be in a notable network with so many marvellous professionals with helpful methods. I feel rather privileged to have come across your entire web site and look forward to tons of more brilliant moments reading here. Thank you once again for a lot of things.

    回复 2019年10月12日
  4. jordan shoes

    My husband and i got absolutely glad that Edward managed to complete his homework through your ideas he grabbed from your own blog. It is now and again perplexing to simply be giving for free secrets which often most people could have been trying to sell. So we do understand we now have the writer to be grateful to because of that. All the illustrations you’ve made, the straightforward website navigation, the relationships you can give support to promote – it’s got all overwhelming, and it is making our son in addition to us reason why this subject matter is interesting, and that’s incredibly mandatory. Thanks for everything!

    回复 2019年10月11日
  5. yeezy 700

    Needed to compose you this very small observation to give thanks again on the striking secrets you have featured at this time. It has been certainly pretty generous with you to convey unhampered just what many individuals could possibly have made available for an e-book to end up making some dough for their own end, precisely since you might well have tried it in the event you wanted. The pointers as well worked to become good way to understand that other people online have a similar dreams much like mine to grasp much more when considering this issue. I’m certain there are numerous more pleasant instances up front for folks who go through your site.

    回复 2019年10月10日
  6. adidas gazelle

    My spouse and i felt lucky that Louis managed to carry out his inquiry by way of the ideas he was given from your very own web site. It is now and again perplexing just to happen to be making a gift of information and facts which often some other people may have been selling. And we also keep in mind we now have the blog owner to be grateful to because of that. These illustrations you made, the simple site menu, the friendships you make it easier to instill – it’s got all superb, and it is facilitating our son in addition to us understand that subject matter is satisfying, and that’s tremendously fundamental. Many thanks for all!

    回复 2019年10月9日
  7. nike air max 2019

    I have to show some thanks to this writer for rescuing me from this type of dilemma. Right after scouting through the world-wide-web and getting opinions which were not productive, I believed my life was well over. Living without the answers to the problems you’ve solved by way of your main posting is a critical case, as well as the ones which might have badly damaged my career if I hadn’t noticed your web site. That talents and kindness in maneuvering all things was valuable. I’m not sure what I would’ve done if I hadn’t encountered such a solution like this. I can also now look ahead to my future. Thanks for your time so much for your professional and sensible guide. I will not hesitate to endorse your site to any individual who will need guidance about this situation.

    回复 2019年10月9日
  8. nike 270

    Thanks so much for providing individuals with remarkably memorable opportunity to read from this website. It’s usually very cool and jam-packed with amusement for me and my office colleagues to search your web site minimum three times in one week to read the new stuff you have. Of course, I’m at all times impressed with your perfect advice you give. Certain two points on this page are honestly the finest we’ve ever had.

    回复 2019年10月8日