使用grep匹配确切的单词 [英] Match exact word using grep

查看:65
本文介绍了使用grep匹配确切的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要搜索确切的单词并打印一行.如果行中没有任何.(点),则该功能正常.

I have a requirement to search for an exact word and print a line. It is working if I don't have any . (dots) in the line.

$cat file
test1 ALL=ALL
w.test1 ALL=ALL

$grep -w test1 file
test1 ALL=ALL
w.test1 ALL=ALL

它也给出了第二行,我只需要具有确切单词 test1 的行.

It is giving the second line also and I want only the lines with the exact word test1.

推荐答案

尝试一下:

grep -E "^test1" file

这表明以单词test1作为第一行的所有内容.现在,如果您需要在行的中间进行查找,这将不起作用,但是您对此并不十分明确.至少在给出^的示例中有效.

This states that anything that starts with the word test1 as the first line. Now this does not work if you need to fin this in the middle of line but you werent very specific on this. At least in the example given the ^ will work.

这篇关于使用grep匹配确切的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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