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.
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$
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With