Grep表达式以匹配空格或行首 [英] Grep expression to match space or start-of-line

查看:587
本文介绍了Grep表达式以匹配空格或行首的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个grep表达式,该表达式可以让我检查标点符号前是否有空格或行首.我想确保标点符号被一致地使用.我所拥有的(为简单起见,我在示例中仅将[\.,;:\!?]替换为,)

I am looking for a grep expression which lets me check if there is a space or a start-of-line before a punctuation mark; I want to make sure that punctuation is used consistently. What I have is this (for simplicity I replaced [\.,;:\!?] with just a , in the example):

~ > echo -e "some, text\nsome ,text\n,some text" | grep " ,"
2:some ,text

没关系,但是错过了第三种情况.我已经尝试过[^ ],"(^\| ),",但是都没有用.正确的表达方式是什么?

That's ok, but misses the third case. I've tried [^ ], and "(^\| )," but neither work. What's the correct expression here?

推荐答案

这将起作用.我认为您不能假设-P选项将可用(默认OSX grep中没有此选项).但是grep -E也可以避免转义:

Here's what'll work. I don't think you can assume the -P option will be available (It's not in the default OSX grep). But grep -E lets you avoid the escaping too:

echo -e "some, text\nsome ,text\n,some text" | grep -E "( |^),"

这篇关于Grep表达式以匹配空格或行首的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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