Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to capture a whole word only using egrep

I am using egrep to look through scripts in our perforce NAS.

i am trying to find where we use RCP & RSH....The problem i have is that 1) I suck at REGEX; 2) i am picking up junk I am not interested in. For example if a file has the word strcpy..it picks up on RCP..or ownership..hits on RSH.

Obviously i am not interested in those, but I don't want to exclude lines based on the words ownership or strcpy...because they may be used in conjunction...and its not a complete list.

Here is my regex

 'ftp|rcp|rsh' 

How can mod these to hit on FTP, but not SFTP...rcp but no strcpy, rsh but not ownership.....etc.?

So Things I would want to match.

ftp
`ftp`
/ftp/
"PUNCT"FTP"PUNCT"
like image 785
nitrobass24 Avatar asked Sep 20 '12 15:09

nitrobass24


People also ask

How do you grep a whole word?

Checking for the whole words in a file : By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.

Which flag to grep causes it to search for whole words?

The easiest of the two commands is to use grep's -w option. This will find only lines that contain your target word as a complete word. Run the command "grep -w hub" against your target file and you will only see lines that contain the word "hub" as a complete word.

What is E in grep?

Matches using extended regular expressions. –e pattern. Specifies one or more patterns separated by newlines for which grep is to search. You can indicate each pattern with a separate –e option character, or with newlines within pattern.


1 Answers

Maybe you need something like this:

\b < - this is the border of word

\bpattern\b <- this pattern will match only pattern, but not otherbigpatternthatyounotneed

like image 136
gaussblurinc Avatar answered Sep 18 '22 15:09

gaussblurinc