Unix day 4: Careful when using grep
How to prevent some common mistakes in grep
1. Searching for special characters
When you have to search some words using grep
and those words have special characters in them, you have to be careful and use the -F
parameter in grep
command
You might want to search for a password which might contain a special character like % @ $
or ;
Lets take an example .
If you have a password file ( lets say pass.txt
) with hashed passwords ( lets say sha1
encryption )
It might contain lines like this :
sha1$e4da8$bf4546fa5fedfb7c1b42727b3f85208d07d95851
and if you look at the count of lines that have 'sha1$'
in it ,
$ grep ‘sha1$’ pass.txt | wc -l
0
you will get 0 as an output. Thats because the $
is treated as a special character . ( In UNIX $
represents the beginning of a variable ). So to escape the special character we use -F
parameter
Unix Solution :
$ grep -F ‘sha1$’ pass.txt | wc -l
23