如何使用 grep 匹配空格或换行符 [英] how to use grep to match with either whitespace or newline
本文介绍了如何使用 grep 匹配空格或换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用一个词 grep 一个文件,比如AAA",它以空格或换行符结尾.我知道如何单独编写它,如下所示,但是在组合它们时遇到问题(从某种意义上说,它同时输出 VVV AAA
和 AAA VVV
).
I want to grep a file with a word, say "AAA", and it ends with whitespace or newlines. I know how to write this seperately, as follows, but having problems in combining them (in the sense that it outputs both VVV AAA
and AAA VVV
).
$echo -e "AAA VVV
VVV AAA
BBB" | grep "AAA$"
>VVV AAA
$echo -e "AAA VVV
VVV AAA
BBB" | grep "AAA[[:space:]]"
>AAA VVV
我曾尝试使用 []
,但没有成功..
I have tried using []
, but without success..
推荐答案
如果您要查找单词 AAA
后跟空格的字符串中任意位置或行尾,请使用>
If you are looking for word AAA
followed by space anywhere in the string, or at the end of line, then use
grep -P "AAA( |$)"
这篇关于如何使用 grep 匹配空格或换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文