Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grepping for exact string while ignoring regex for dot character

Tags:

grep

bash

So here's my issue. I need to develop a small bash script that can grep a file containing account names (let's call it file.txt). The contents would be something like this:

accounttest
account2
account
accountbtest
account.test

Matching an exact line SHOULD be easy but apparently it's really not.

I tried:

grep "^account$" file.txt

The output is:

account

So in this situation the output is OK, only "account" is displayed.

But if I try:

grep "^account.test$" file.txt

The output is:

accountbtest
account.test

So the next obvious solution that comes to mind, in order to stop interpreting the dot character as "any character", is using fgrep, right?

fgrep account.test file.txt

The output, as expected, is correct this time:

account.test

But what if I try now:

fgrep account file.txt

Output:

accounttest
account2
account
accountbtest
account.test

This time the output is completely wrong, because I can't use the beginning/end line characters with fgrep.

So my question is, how can I properly grep a whole line, including the beginning and end of line special characters, while also matching exactly the "." character?

EDIT: Please note that I do know that the "." character needs to be escaped, but in my situation, escaping is not an option, because of further processing that needs to be done to the account name, which would make things too complicated.

like image 557
Tony Avatar asked Jan 30 '23 08:01

Tony


2 Answers

The . is a special character in regex notation which needs to be escaped to match it as a literal string when passing to grep, so do

grep "^account\.test$" file.txt

Or if you cannot afford to modify the search string use the -F flag in grep to treat it as literal string and not do any extra processing in it

grep -Fx 'account.test' file.txt

From man grep

-F, --fixed-strings

Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.

-x, --line-regexp

Select only those matches that exactly match the whole line. For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ^ and $.

like image 162
Inian Avatar answered Feb 05 '23 15:02

Inian


fgrep is the same as grep -F. grep also has the -x option which matches against whole lines only. You can combine these to get what you want:

grep -Fx account.test file.txt
like image 38
camh Avatar answered Feb 05 '23 17:02

camh