在bash的grep有趣的比赛 [英] Interesting grep match in bash

查看:94
本文介绍了在bash的grep有趣的比赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能解释为什么

这人给$? = 1

 回声UUS| grep的-w -o [0123456789] \\ *。

和这个人给$? = 0

 回声-uus| grep的-w -o [0123456789] \\ *。


解决方案

您定期的前pression可以匹配一个空字符串。在 -w 标志意味着任何比赛必须由年初的行或一个非文字字符pceded $ P $,其次为尾行或非单词字符。

UUS 的情况下,行的开头后面是一个单词字符,所以的grep 可以'匹配空字符串,因为一个字。行的结尾是一个文字字符pceded $ P $,所以的grep 无法比拟的一个空字符串,因为一个字。

-uus 的情况下,行的开头后跟 - ,这是一个非单词字符,所以的grep 可以匹配空字符串作为该行的开头和之间的一个字 - 字符。

Can you explain why

This one gives $? = 1

echo "uus" | grep -w -o [0123456789]\*

and this one give $? = 0

echo "-uus" | grep -w -o [0123456789]\*

解决方案

Your regular expression can match an empty string. The -w flag means that any match must be preceded by beginning-of-line or a non-word character, and followed by end-of-line or a non-word character.

In the case of uus, the beginning of line is followed be a word character, so grep can't match an empty string as a word there. The end of line is preceded by a word character, so grep can't match an empty string as a word there.

In the case of -uus, the beginning of line is followed by -, which is a non-word character, so grep can match the empty string as a word between the beginning of the line and the - character.

这篇关于在bash的grep有趣的比赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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